请您帮我选择与下面其他行相同的最后一行?
表格如下:
ID | NAME | VALUE | DATE |
1 | test | 1 | 2018 |
2 | test | 1 | 2018 |
14 | test | 0 | 2018 |
5 | test | 0 | 2018 |
7 | test | 0 | 2018 |
8 | test | 0 | 2018 |
9 | test | 0 | 2018 |
10 | test | 0 | 2018 |
但我想选择所有值为1,第一个值为0 所以结果应该是这样的:
ID | NAME | VALUE | DATE |
1 | test | 1 | 2018 |
2 | test | 1 | 2018 |
14 | test | 0 | 2018 |
答案 0 :(得分:0)
使用UNION ALL将记录与值= 1组合,最后一条记录与值= 0. LIMIT 1将仅显示最后一条记录。
SELECT *
FROM yourTable
WHERE `value` = 1
UNION ALL
SELECT *
FROM (SELECT *
FROM yourTable
WHERE `value` = 0
ORDER BY ID DESC
LIMIT 1) tab