[英]MYSQL - JOIN the 1 table or the other table depending if there is a match
I have a table called events
and I am doing a select on this table to display a grid with the select results. 我有一个称为
events
的表,正在对此表进行选择以显示带有选择结果的网格。
The events
table has a column called s_code
. events
表中有一列称为s_code
。 The s_code
value comes from either the suppliers
or members
table. s_code
值来自suppliers
表或members
表。
How would I do a single JOIN
that checks the suppliers
and members
table to get the code's name without having to do a LEFT JOIN
on both members
and suppliers
tables like my query below. 我将如何执行单个
JOIN
检查suppliers
和members
表以获取代码的名称,而不必像下面的查询那样对members
和suppliers
表都进行LEFT JOIN
。 Basically I only want to have b.s_name
where I have both b.s_name
and c.s_name
at the moment. 基本上,我只想拥有
b.s_name
,而此时我同时拥有b.s_name
和c.s_name
。
SELECT a.s_id, b.s_name, c.s_name, a.s_date,
a.s_description, d.s_name, a.s_actiondate,
e.s_name, a.s_emailed, a.s_status
FROM events AS a
LEFT JOIN members AS b ON a.s_code = b.s_code
LEFT JOIN suppliers AS c ON a.s_code = c.s_code
LEFT JOIN webuser AS d ON a.s_userid = d.s_ai
LEFT JOIN webuser AS e ON a.s_actionuserid = e.s_ai
WHERE a.s_status = 'A'
AND a.s_userid = '1'
AND a.s_transactionstatus = 'A'
刚刚凝聚的两列进一个,如果第一个是非零回报,否则第二;
SELECT a.s_id, COALESCE(b.s_name, c.s_name) s_name, a.s_date, ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.