我正在尝试为每个组获得一个结果,但它返回的不止一个。在这里我的发言
SELECT in_id, in_name, ig_id, ig_name, di_name, iv_id, iv_reading, iv_date, iv_time
FROM `instruments`
JOIN instrument_group ON ig_id = in_igid
LEFT JOIN instrument_reading ON iv_inid = in_id AND date(iv_date) = date(20130301)
RIGHT JOIN dam_info ON di_id = ig_diid
WHERE ig_id = in_igid
这里是输出
如上所示,in_id
6有两个结果,我只想要一个表instrument_reading
的最新结果。
更新:表架构
非常感谢您的帮助。
答案 0 :(得分:0)
SELECT in_id, in_name, ig_id, ig_name, di_name, iv_id, iv_reading, iv_date, iv_time
FROM `instruments`
JOIN instrument_group ON ig_id = in_igid
LEFT JOIN (SELECT iv_id, iv_inid, iv_reading,iv_date, iv_time FROM instrument_reading WHERE date(iv_date) = date(20130301) ORDER BY iv_time DESC LIMIT 1) as latestDt ON latestDt.iv_inid = in_id
LEFT JOIN dam_info ON di_id = ig_diid
WHERE ig_id = in_igid
我用它作为临时解决方案。如果你有更好的方法,我会打开你的