gpt4 book ai didi

MySQL : double IN() statement

转载 作者:行者123 更新时间:2023-11-29 04:19:23 26 4
gpt4 key购买 nike

driver [ dcode, dname ]
route [ rcode, departure,arrival ]
ride [ dcode, rcode ]

SELECT dname FROM driver WHERE dcode IN (
SELECT dcode FROM ride WHERE rcode IN (
SELECT rcode FROM route WHERE departure = 'Barcelona' AND arrival = 'Madrid'
) AND
rcode IN (
SELECT rcode FROM route WHERE departure = 'Madrid' AND arrival= 'Barcelona'
)
);

它是关于一位同时走两条路线的司机:马德里-巴塞罗那和巴塞罗那-马德里,但我的查询不起作用。

最佳答案

where in 是错误的方法,你要找的是 where exists

SELECT dname FROM driver WHERE exists
( select dcode from route inner join ride on route.rcode = ride.rcode
where departure = 'Barcelona' and arrival = 'Madrid' and ride.dcode = driver.dcode
)
and exists
( select dcode from route inner join ride on route.rcode = ride.rcode
where departure = 'Madrid' and arrival = 'Barcelona' and ride.dcode = driver.dcode
)

或者,您也可以完全不使用子查询:

select dname 
from driver d
inner join ride r
on d.dcode = r.dcode
inner join route rr
on r.rcode = rr.rcode
and rr.departure = 'Barcelona'
and rr.arrival = 'Madrid'
inner join ride r2
on d.dcode = r.dcode
inner join route rr2
on r2.rcode = rr2.rcode
and rr2.departure = 'Madrid'
and rr2.arrival = 'Barcelona';

demo here

关于MySQL : double IN() statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30008557/

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