数据库名称: MySQL
内容/问题描述:
SELECT * FROM Table1 as a
LEFT JOIN Table2 as b
ON a.key = b.key AND b.id IN (1,2)
WHERE ....
我只想JOIN Table2 里符合资料的第一笔
如果id = 1符合的话 join id = 1 的资料
不符的话才是id = 2 的
用何种方式会比较好呢?
本来想再JOIN一次,然后
SELECT * , (CASE b.id WHEN null THEN b.name ELSE c.name END) as name
FROM Table1 as a
LEFT JOIN Table2 as b
ON a.key = b.key AND b.id = 1
LEFT JOIN Table2 as c
ON a.key = b.key AND b.id = 2
WHERE ...
不过Table2要抓出的字段有十多个,这样除了麻烦外好像也会拖累效率?
而且要是id再多一个选项的话,条件会更天荒地老吧 @.@