select t.*
from talk t
where t.user_id = 2
Union
select t.*
from talk t, user_contact c
where c.user_id = 2 and c.contact_type = 1 and c.be_user_id = t.user_id
使用Union这么写是正确的
然后用左连接我就不会写了,怎么写都会有重复数据
select t.*
from talk t
where t.user_id = 2
Union
select t.*
from talk t, user_contact c
where c.user_id = 2 and c.contact_type = 1 and c.be_user_id = t.user_id
使用Union这么写是正确的
然后用左连接我就不会写了,怎么写都会有重复数据
从你给的例子,如果最终结果只取链接中主表的数据,且要求不重复,就不应该用左连接。
应该使用exists,如:
select *
from a
where exists (select 1 from b where a.uid = b.uid or a.beUid=b.id)
15 回答8.2k 阅读
8 回答6k 阅读
5 回答3.3k 阅读✓ 已解决
3 回答3.6k 阅读✓ 已解决
1 回答4.1k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答2.8k 阅读✓ 已解决
我能想到的就是union