資料庫名稱: 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再多一個選項的話,條件會更天荒地老吧 @.@