sql连表查询,查询A中数据B中是否存在

SELECT
o. ID AS ID,
(
CASE
WHEN q.uniscid = o.uniscid THEN
q.uniscid
ELSE
NULL
END
) AS uniscid,
(
CASE
WHEN q.regno = o.regno THEN
q.regno
ELSE
NULL
END
) AS regno,
(
CASE
WHEN q.entname = o.entname THEN
q.entname
ELSE
NULL
END
) AS entname
FROM
data_syfr_tem o
LEFT JOIN data_qyfr q ON (
o.entname = q.entname
OR o.uniscid = q.uniscid
OR o.regno = q.regno
)
WHERE
o.batchno = '20171213-2'
AND STATE = '1';


图片说明
我想把下面的2个6077合成 这种数据》》6077 1211 null 测试单位名称
有大哥能指点下吗?

 select o.id,q.uniscid,q.regno,q.entname from data_syfr_tem o LEFT JOIN data_qyfr q on   q.uniscid = o.uniscid  and q.regno = o.regno 
and q.entname = o.entname  where o.batchno = '20171213-2' AND STATE = '1';

在代码里做逻辑处理不行么。。

你说的这是sql full join
http://www.yiibai.com/sql/sql-full-joins.html

不理解你的SQL为什么要这样写。
select o.id,q.uniscid,q.regno,q.entname from o join q on (条件)where ... 就可以完成了

至于你的需求是把两条数据合并为1条,你确定要这么做么?
{6077 null null 测试单位名称 6077 1211 null null} -->6077 1211 null 测试单位名称

你可以直接两个表连接查询,判断A表值有的情况下,B表对应值是否为空;这样还会直接去除你的SQL当中出现的重复值
还有看你的逻辑是三个字段有一个相等的就会出现在待查询的结果集当中,那很大可能性是有重复的数据的,但是针对你符合条件时候查询结果,
其他字段怎么合在一起,也没看出来你有什么固定的逻辑,针对不确定的字段值,没法做合并;
如果要根据结果集,针对某一字段相同需要拼合,你可以对结果进行group,并且其他字段直接拼合,

select o.id,(select uniscid from q where q.uniscid=o.uniscid),(select regno from q where q.regno=o.regno),(select entname from q where q.entname cid=o.entname ) from o join q on (条件)where ...

SELECT * from tabel group by id where uniscid <> null

重点是group by

这样做还不如查询之后在程序里面判断来的快,你这样有点耗资源

sql 中有exists 和not exists函数, 使用这个

这种情况下,你要取什么样的数据

6700  1211  null  null
6700  null  null  测试
6700  1111  2222  3333