In Table 1, the first five values are sorted according to a certain colume value, in Table 2, the first five values are sorted according to the same colume value, and the first five values are retrieved from the data in the two tables. How do you write this operation?

  mysql, question

For example, table 1 table_1, three field id, name, time
Table 2 table_2, three field id, name, time
Respectively look up the maximum value of time in the table, then take the larger of the two and label it as coming from the table.

Ask yourself and answer:
Select
temp.id, temp.time, temp.ttable
From
((SELECT
id, time, ‘1’ ttable
From
table_1
ORDER BY time DESC
LIMIT 5) UNION ALL (SELECT
id, time, ‘2’ ttable
From
table_2
ORDER BY time DESC
LIMIT 5)) AS temp
ORDER BY time DESC
LIMIT 5