SELECT *, (SELECT name FROM tbl_b WHERE tbl_b.id1 = tbl_a.id1 AND tbl_b.id2 = tbl_a.id2 ORDER BY date DESC LIMIT 1) as name, (SELECT detail FROM tbl_b WHERE tbl_b.id1 = tbl_a.id1 AND tbl_b.id2 = tbl_a.id2 ORDER BY date DESC LIMIT 1) as detail FROM tbl_a WHERE id1='1';
SQL Server可以这样写的,减少一半IO: SELECT * FROM tbl_a CROSS APPLY (SELECT TOP (1) name, detail FROM tbl_b WHERE id1=tbl_a.id1 AND id2=tbl_a.id2 ORDER BY date DESC) AS T WHERE id1='1'
SELECT tbl_a.*, tbl_b.name, tbl_b.detail FROM tbl_a JOIN tbl_b ON tbl_b.id1 = tbl_a.id1 AND tbl_b.id2 = tbl_a.id2 WHERE tbl_a.id1='1' ORDER BY tbl_b.date DESC LIMIT 1
SELECT *, tmp.name, tmp.detail FROM tbl_a, (SELECT name, detail FROM tbl_b WHERE tbl_b.id1 = tbl_a.id1 AND tbl_b.id2 = tbl_a.id2 ORDER BY date DESC LIMIT 1 ) as tmp, WHERE tbl_a.id1='1';
SELECT *, tmp.name, tmp.detail FROM tbl_a, (SELECT name, detail FROM tbl_b JOIN tbl_a ON tbl_b.id1 = tbl_a.id1 AND tbl_b.id2 = tbl_a.id2 ORDER BY date DESC LIMIT 1 ) as tmp WHERE tbl_a.id1='1';
第 1 页 / 共 1 页
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。