S
S
studprogrammist2018-04-13 04:44:58
SQL
studprogrammist, 2018-04-13 04:44:58

How to implement SQL query, select by date?

Compiled such request for selection of values ​​from two tables (fields in which have communication "one to many"). At the same time, I get only those values ​​​​that have the oldest "key", that is, they were recorded last.

SELECT tab1.indexObject, tab1.ObjectNames, tab2.XMLizmerenije, tab2.XMLdate, tab2.dateMessage, max(tab2.dateMessage) FROM tab1 JOIN tab2
ON (tab1.keyID = tab2.indexObjectsTab_id)
WHERE tab2.dateMessage > 0 and tab1.indexObject  = 11222

At the same time, everything works, but I would also like to learn how to select by date (dateMessage). But something does not work correctly to make a request. Tell me how to implement it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexalexes, 2018-04-13
@alexalexes

At the same time, I get only those values ​​\u200b\u200bthat have the highest "key"

Your query does not select the most-most, your query selects something with a fixed parameter:
WHERE tab2.dateMessage > 0 and tab1.indexObject = 11222
To take the most-most, for example, by date, the selection needs to be sorted .
Please note that you can sort by several columns, and not only by columns.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question