SELECT
'1' AS SITE
, 'test_id' AS ID
, IFNULL(PREV_DATA, 60) AS PREV_DATA
, IFNULL(PREV_DATA + 2, 60 + 2) AS DATA
FROM 'TABLE'
WHERE ID = 'test_id'
ORDER BY CREATE_DATE DESC
LIMIT 1;
即使没有找到行,我也只想要一行
订单非常重要。
我尝试了COUNT(1)或MAX(1),但结果没有按create_date排序...
请帮忙。
-----添加样本数据
+------+---------+-----------+------+---------------------+ | SITE | ID | PREV_DATA | DATA | CREATE_DATE | +------+---------+-----------+------+---------------------+ | 1 | test_id | 60 | 62 | 2016-03-21 00:00:00 | | 1 | test_id | 62 | 64 | 2016-03-21 10:00:00 | | 1 | test_22 | 60 | 62 | 2016-03-24 00:00:00 | +------+---------+-----------+------+---------------------+
当ID为'test_id'时,我想要下面的数据。
+------+---------+-----------+------+ | SITE | ID | PREV_DATA | DATA | +------+---------+-----------+------+ | 1 | test_id | 64 | 66 | +------+---------+-----------+------+
当id为'test33333'时,我想要下面的数据
+------+-----------+-----------+------+ | SITE | ID | PREV_DATA | DATA | +------+-----------+-----------+------+ | 1 | test33333 | 60 | 62 | +------+-----------+-----------+------+
答案 0 :(得分:0)
查看UNION ..所以你选择了一个额外的预定义或"空白"行但有限制1,它只显示何时找不到另一行
类似的东西(未经测试):
SELECT
'1' AS SITE
, 'test_id' AS ID
, IFNULL(PREV_DATA, 60) AS PREV_DATA
, IFNULL(PREV_DATA + 2, 60 + 2) AS DATA
, CREATE_DATE
FROM 'TABLE'
WHERE ID = 'test_id'
union
SELECT
'0' AS SITE
, '' AS ID
, '' AS PREV_DATA
, '' AS DATA
, DATE_SUB(NOW(), INTERVAL -300 YEAR) as CREATE_DATE
ORDER BY CREATE_DATE DESC
LIMIT 1;