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)
8 回答6.6k 阅读
4 回答715 阅读✓ 已解决
2 回答3.4k 阅读
1 回答2.6k 阅读✓ 已解决
5 回答1.5k 阅读
3 回答1.9k 阅读✓ 已解决
1 回答2.2k 阅读✓ 已解决
我能想到的就是union