我想做的使用LEFT JOIN
(請不要建議UNION ALL
)以下MySQL的條件加入使用同一個表
SELECT o.*, s.col1, s.col2 FROM order o
INNER JOIN user u ON o.user_id = u.id
IF o.date less than '2011-01-01'
JOIN subscribe s ON u.key = s.key
ELSE
JOIN subscribe s ON u.email = s.email
END IF;
我用下面的,但不能對其進行測試。
SELECT o.*, COALESCE(s1.col1,s2.col1) AS
col1, COALESCE(s1.col2, s2.col2) AS col2
FROM order o INNER JOIN user u ON o.user_id = u.id
LEFT JOIN subscribe s1 ON
(u.key LIKE (CASE o.date >= '2011-01-01 00:00:00'
WHEN TRUE THEN s1.key ELSE NULL END))
LEFT JOIN subscribe s2 ON (u.email LIKE (CASE o.date <
'2011-01-01 00:00:00' WHEN TRUE THEN s.email
ELSE NULL END));
如果我錯了,請糾正我。
謝謝。
什麼導致您無法測試您的代碼? –
,因爲我沒有測試環境設置 – arun