我有两张桌子:
表1:
| ID | numero | apagado
| 1 | 23 | 0
| 2 | 56 | 0
| 3 | 156 | 0
| 4 | 48 | 0
表二:
| ID_Table_1 | data_inspecao |
| 1 | 2014-01-03 |
| 2 | 2014-01-08 |
| 1 | 2014-02-20 |
| 4 | 2014-01-06 |
| 2 | 2014-01-23 |
我想得到所有“数字”的最新“数据”。
在我的示例中,我希望得到以下结果:
| ID_Table_1 | data_inspecao |
| 1 | 2014-02-20 |
| 2 | 2014-01-23 |
| 4 | 2014-01-06 |
我有下面的SELECT语句,但是如果我将'ASC'的'DESC'改为'ASC',结果是相同的,我没有得到最新的'data_inspecao':
SELECT
e.numero, i.data_inspecao
FROM
table_1 e
INNER JOIN
table_2 i ON i.ID_Table_1 = e.numero
WHERE
e.numero = 6 AND e.apagado = 0
GROUP BY
e.numero
ORDER BY
i.data_inspecao DESC
如有任何帮助,我们将不胜感激
最佳答案
使用添加MAX
聚合的示例
SELECT
e.numero, Max(i.data_inspecao) as MostRecentDate
FROM
table_1 e
INNER JOIN
table_2 i ON i.ID_Table_1 = e.numero
GROUP BY
e.numero
ORDER BY
e.numero