我有这个问题:
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',任何人都可以提供帮助吗?
this is my table column
答案 0 :(得分:0)
从不在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;
答案 1 :(得分:0)
首先让我尝试了解您希望查询执行的操作:
st
st.st_id = at.at_id
st.st_id = at.st_id
则at.status = status
其他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;
答案 2 :(得分:0)
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`
答案 3 :(得分:0)
/ 如果DB是MYSQL /
,请使用IFNULLSELECT 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;