mysql两个表,一个表的数据是全的,另外一个表是有记录才存储,没有数据就不存储,现在要查询本表的数据有哪些在另外一个表中没有出现的记录。
t_competition表

t_comp_file表

t_competition的pk_id 和t_comp_file 表的fk_comp_id
第一种写法:
SELECT
f_name,
f_protocol_date,
f_link_man,
f_link_tel,
f_email,
f_mobile
FROM
t_competition AS a
WHERE
pk_id NOT IN (
SELECT
fk_comp_id
FROM
t_comp_file AS b
WHERE
f_audit_status = 1
AND f_type = 4
)
AND a.f_audit_status = 3
AND LEFT (f_protocol_date, 4) = 2019
第二种写法:
select f_name,f_protocol_date,f_link_man,f_link_tel,f_email,f_mobile from t_competition as a where (
select count(1) from t_comp_file as b where a.pk_id=b.fk_comp_id and b.f_audit_status=1 and b.f_type=4
)=0 and a.f_audit_status=3 and left(f_protocol_date,4)=2019
本文介绍两种方法来查询MySQL数据库中一个表的数据,在另一个表中没有出现的记录。第一种方法使用NOT IN子句,第二种方法使用子查询与COUNT函数结合。这两种方法适用于需要找出未被关联的数据场景。

被折叠的 条评论
为什么被折叠?



