[英]Sql left join and show result
欢迎,
我有2个表int_client
和int_client_bank
。
在第一重要列中: client_id, firstname, lastname, country, email, status
。 在第二个中: client_id, status
。
我需要显示所有记录:第一列的firstname, lastname, country, e-mail WHERE status='25'
(第二张表中出现client_id
那些记录除外)。 如果出现在第二个表中的client_id
正在跳过第一列的状态,并且在secound表中仅显示status='25'
。
SB能帮我吗?
编辑:
SELECT firstname, lastname, country, email, COALESCE(b.status, a.status) status
FROM int_client a
LEFT JOIN int_client_back b USING (client_id)
WHERE
(b.status IS NULL and a.status='25')
OR b.status IS NOT NULL;
这是很好的代码,但是我只需要在B列中显示状态为“ 25”的记录,如果a.client_id!= b.client_id,我需要在A列中显示状态为“ 25”的记录
SB,帮忙吗?
使用COALESCE()
LEFT JOIN
应该执行以下操作:
SELECT firstname, lastname, country, email, COALESCE(b.status, a.status) status
FROM int_client a
LEFT JOIN int_client_back b USING (client_id)
WHERE
(b.status IS NULL and a.status='25')
OR b.status IS NOT NULL;
此选择查询应工作:
SELECT t1.first_name,t1.last_name,t1.country,t1.email FROM int_client AS t1, int_client_bank AS t2
WHERE t1.client_id = t2.client_id AND t1.status = 25;
下面的查询将为您解决问题。
SELECT client_id, firstname, lastname, country, email, status FROM int_client as ic LEFT JOIN int_client_back icb ON ic.client_id=icb.client_id WHERE ic.status=25
如果我理解正确,则需要int_client
中状态为“ 25”的所有记录。 但是,如果int_client_bank
存在另一个状态,那么您想显示该状态。
select c.first_name, c.last_name, c.email, c.country,
coalesce(cb.status, c.status)
from int_client c left join
int_client_bank cb
on cb.client_id = c.client_id
where c.status = 25;
SELECT DISTINCT firstname, lastname, country, email,
a.status as status_a, b.status as status_b, if(b.status is NULL, a.status, b.status) as final_status
FROM int_client a
LEFT JOIN int_client_bank b USING (client_id)
WHERE
if(b.status is NULL, a.status, b.status) = '25'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.