如何解决mysql超过15个子查询“子查询返回超过1行”的错误

时间:2016-05-13 08:03:15

标签: mysql database

SELECT et.*,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_waittime, 
(SELECT el.mode FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_mode, 
(SELECT el.time FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_fare,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_waittime, 
(SELECT el.mode FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_mode, 
(SELECT el.time FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_fare,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,2) AS leg3_waittime, 
(SELECT el.mode FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,2) AS leg3_mode, 
(SELECT el.time FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,2) AS leg3_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,2) AS leg3_fare,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,3) AS leg4_waittime, 
(SELECT el.mode FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,3) AS leg4_mode, 
(SELECT el.time FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,3) AS leg4_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,3) AS leg4_fare
FROM entitytrip as et

2 个答案:

答案 0 :(得分:0)

不试图了解你想要做什么,我发现最后8个子查询的限制为1,2或1,3。这些子查询将返回多行,因此您最好将其更改为2,1& 3,1



SELECT et.*,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_waittime, 
(SELECT el.mode FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_mode, 
(SELECT el.time FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_fare,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_waittime, 
(SELECT el.mode FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_mode, 
(SELECT el.time FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_fare,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 2,1) AS leg3_waittime, 
(SELECT el.mode FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 2,1) AS leg3_mode, 
(SELECT el.time FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 2,1) AS leg3_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 2,1) AS leg3_fare,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 3,1) AS leg4_waittime, 
(SELECT el.mode FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 3,1) AS leg4_mode, 
(SELECT el.time FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 3,1) AS leg4_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 3,1) AS leg4_fare
FROM entitytrip as et




答案 1 :(得分:0)

SELECT et.*,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_waittime, 
(SELECT el.`mode` FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_mode, 
(SELECT el.`time` FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1) AS leg1_fare,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_waittime, 
(SELECT el.`mode` FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_mode, 
(SELECT el.`time` FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 1,1) AS leg2_fare,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 2,1) AS leg3_waittime, 
(SELECT el.`mode` FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 2,1) AS leg3_mode, 
(SELECT el.`time` FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 2,1) AS leg3_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 2,1) AS leg3_fare,
(SELECT el.waitTime FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 3,1) AS leg4_waittime, 
(SELECT el.`mode` FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 3,1) AS leg4_mode, 
(SELECT el.`time` FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 3,1) AS leg4_time, 
(SELECT el.fare FROM entityleg as el WHERE et.tripId=el.tripId LIMIT 3,1) AS leg4_fare
FROM entitytrip as et

这是更正后的代码。由于时间模式已在mysql中预定义。因此,要让mysql将其理解为列名,您必须使用`time` `mode`

同样出现错误“子查询返回超过1行”,因为您错放了限制和偏移量。而不是limit 2,1您使用了limit 1,2。同样limit 3,1您使用了limit 1,3