gpt4 book ai didi

MySQL - 加入同一张表两次

转载 作者:太空宇宙 更新时间:2023-11-03 12:34:20 25 4
gpt4 key购买 nike

我有一个名为“预订”的表

> id     status 
1 P
2 P

还有一个叫做'call'

id    calldate    type   booking
1 01/01/2012 DEL 1
2 01/02/2012 COL 1
3 01/03/2012 DEL 2
4 31/12/2019 COL 999

我想一次列出“预订”中的每条记录,将来自“调用”的相关记录显示为另一列,如下所示:

bookingId    deliverydate  collectiondate
1 01/01/2012 01/02/2012
2 01/03/2012 null

我试过:

select `b`.`bookingid` AS `bookingid`,
`del`.`calldate` AS `Delivery`,
`col`.`calldate` AS `Collection`
from `booking` `b`
left join `call` `del` on `b`.`bookingid` = `del`.`booking`
left join `call` `col` on `b`.`bookingid` = `col`.`booking`
where ((`del`.`type` = 'DEL') OR (`col`.`type` = 'COL') and (`b`.`status` = 'P'));

但我得到 bookingid 1 列出 3 次。有人可以修复我的连接吗?

最佳答案

我认为您想将您的类型移动到连接条件中:

select `b`.`bookingid` AS `bookingid`,
`del`.`calldate` AS `Delivery`,
`col`.`calldate` AS `Collection`
from `booking` `b`
left join `call` `del` on `b`.`bookingid` = `del`.`booking` AND `del`.`type` = 'DEL'
left join `call` `col` on `b`.`bookingid` = `col`.`booking` AND `col`.`type` = 'COL'
where `b`.`status` = 'P';

关于MySQL - 加入同一张表两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13943350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com