W
W
wrietr2016-01-22 10:55:51
MySQL
wrietr, 2016-01-22 10:55:51

How to compose sql?

Tell me how to do it? already broke the whole brain
[data ] there is a table with data
id | value | year | month | day | hour ...
[ lastadata ] there is a table with data that has already been checked and it contains the id of the last checked record
id | id_data
how to get the data and if there is no data, then get the last checked ones instead,
for example
[data]
1 | 120 | 2015 | 12 | 10 | 2 |
2 | 121 | 2015 | 12 | 10 | 3 |
3 | 122 | 2015 | 12 | 10 | 4 |
4 | 122 | 2015 | 12 | 10 | 5 |
[lastdata]
1 | 2 <- link
As a result, get
120 | 2015 | 12 | 10 | 2 |
121 | 2015 | 12 | 10 | 3 |
122 | 2015 | 12 | 10 | 4 |
121 | 2015 | 12 | 10 | 3 |
122 | 2015 | 12 | 10 | 5 |
simple examples

SELECT * FROM data WHERE year=<...> AND mont=<...> AND day = <...>


SELECT * FROM lastdata LEFT JOIN data ON data.id = lastdata.id_data WHERE <...>

EXPLANATIONS ON APPLES
Vasya had 16 apples 2 weeks ago. Then Vasya was ill for a week. Then he recovered and began to say how many apples he had. Therefore, for the period of illness, we need to show how many apples he had - that is, the last one that we received from him. Roughly speaking, fill in the gap

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vadim_tur, 2016-01-22
@vadim_tur

As I understand from the last example, you just need to add the lust_update field to the table, the default TIMESTAMP type is CURRENT_TIMESTAMP with the on update attribute. And update the same table row. And you will see how many apples the user has and when he will last inform you about it.
It is suitable? or do you need to save the history of changes in the number of apples from the user?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question