如何根据不同的连接表条件从不同的表中获取值

ct3nt3jp  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(136)

我有三张table。
客户-姓名、电子邮件
历史记录-id、客户id、与会者id、日期
与会者-姓名、电子邮件
我正在尝试使用历史记录表获取姓名和电子邮件,如果customer\u id为-1,则需要从attendee表获取姓名和电子邮件,或者从customer表获取姓名和电子邮件,如下所示:

SELECT  * FROM history

left JOIN attendee ON attendee.id  = history.attendee_id AND history.customer_id= '-1'

left JOIN customer ON customer.id = history.customer_id AND history.customer_id != '-1'

WHERE history.id = '605';

我可以用union all做同样的事情,但不用join
工会代码如下

select cus.name,cus.email from histroy
join customer cus on cus.id = history.customer_id and history.customer_id not in ('0','-1')
union all
select cus.name,cus.email from history
join attendee cus on cus.id = history.attendee_id and history.attendee_id not in ('0','-1')

我可以用join写上面的代码吗。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题