[英]MySQL Query LEFT OUTER JOIN AND IFNULL
我有这个查询:
SELECT st.st_id, st.id, st.name, at.status, at.date
FROM st,at
WHERE st.st_id = at.at_id;
但我想要的结果返回所有的st
表,如果st
在.st_id存在at
.st_id,状态栏就是在at
表中,如果没有状态为“H”,任何人都可以帮忙吗? 这是我的表格列
/如果 DB 是 MYSQL,则使用 IFNULL /
SELECT st.st_id, st.id, st.name, IFNULL(at.status,'H') as status, at.date
FROM st LEFT JOIN
at
ON st.st_id = at.at_id;
切勿在FROM
子句中使用逗号。 始终使用正确、明确的JOIN
语法。 然后编写查询会更容易:
SELECT st.st_id, st.id, st.name, COALESCE(at.status, 'H') as status, at.date
FROM st LEFT JOIN
at
ON st.st_id = at.at_id;
首先让我尝试了解您希望查询做什么:
st
where st.st_id = at.at_id
st.st_id = at.st_id
那么at.status = status
else status='H'
我建议的脚本:
SELECT st.st_id, st.id, st.name, IF(st.st_id = at.st_id, at.status, 'H') AS status, at.date FROM st LEFT JOIN at ON st.st_id = at.at_id;
SELECT st.st_id,
st.id,
st.name,
st.cl_id,
st.gender,
CASE WHEN ISNULL(at.st_id ) THEN 'H' ELSE at.status
FROM `st`
LEFT JOIN `at` ON `st`.`st_id` = `at`.`st_id`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.