left join的关联条件有两种可能,可能是a.uid=b.uid也有可能a.beUid=b.id这时候怎么写?

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这么写是正确的

然后用左连接我就不会写了,怎么写都会有重复数据

阅读 2.1k
2 个回答

我能想到的就是union

从你给的例子,如果最终结果只取链接中主表的数据,且要求不重复,就不应该用左连接。
应该使用exists,如:

select *
from a
where exists (select 1 from b where a.uid = b.uid or a.beUid=b.id)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题