子查询返回多个字段

时间:2016-01-04 06:57:13

标签: php mysql

我有这样的表格结构 Table Structure

在这里,我想从bus_routines表中选择总线详细信息和avaliable_seat的所有数据,该数据是从buses.number_of_seat - reserved_seats.number_of_reserved_seat - booking.number_of_seat计算的,即使{{1}中没有数据} table和booking表,其中 bus_routines.sector_from = Ktm,bus_routines.sector_to = Pkr和bus_routines.date = 2015-12-15

它们之间的关系是:

  

bus和bus_routines - >一对多的bus_routines和预订 - >   一对多的bus_routines和reserved_seats - >一对多

我尝试了以下查询

reserved_seats

我得到了我想要的结果,但SELECT r.* , b.* , ( SELECT b.number_of_seat - sum(booking.number_of_seat)-sum(reserved_seats.number_of_reserved_seat) FROM bus_routines AS r INNER JOIN buses AS b ON b.id = r.bus_id INNER JOIN (SELECT number_of_seat , bus_routine_id FROM booking GROUP BY booking.bus_routine_id) AS booking ON booking.bus_routine_id = r.id INNER JOIN (SELECT number_of_reserved_seat , routine_id FROM reserved_seats GROUP BY reserved_seats.routine_id) AS reserved_seats ON r.id = reserved_seats.routine_id WHERE r.sector_from = "KTM" AND r.sector_to = "PKR" AND r.departure_date = "2015-12-15" ) AS avaliable_seat FROM bus_routines AS r INNER JOIN buses AS b ON b.id = r.bus_id WHERE r.sector_from = "KTM" AND r.sector_to = "PKR" AND r.departure_date = "2015-12-15" HAVING avaliable_seat > 0 对于所有行

是相同的

我也尝试了另一个查询,但它给了我单一的结果

avaliable_seat

我还尝试了另一个查询,它给了我Subquery返回超过1行。查询是

SELECT r.* , b.* , b.number_of_seat - sum(booking.number_of_seat)-sum(reserved_seats.number_of_reserved_seat) AS available_seat

FROM bus_routines AS r 
INNER JOIN buses AS b 
    ON b.id = r.bus_id 
INNER JOIN 
    (SELECT number_of_seat , bus_routine_id FROM booking GROUP BY booking.bus_routine_id) AS booking 
    ON booking.bus_routine_id = r.id 
INNER JOIN 
    (SELECT number_of_reserved_seat , routine_id FROM reserved_seats GROUP BY reserved_seats.routine_id) AS reserved_seats 
    ON r.id = reserved_seats.routine_id
WHERE   
    r.sector_from = "KTM" AND 
    r.sector_to = "PKR" AND 
    r.departure_date = "2015-12-15" 
HAVING available_seat > 0

2 个答案:

答案 0 :(得分:0)

一种方法是使用相关子查询来获取每个reserved_seats的{​​{1}}和booked_seats

我们假设此查询返回您要返回的行,它只是缺少您想要计算的bus_routine列:

available_seat

预约席位的数量"对于给定的bus_routine,您可以查询SELECT r.* , b.* FROM bus_routines r JOIN buses b ON b.id = r.bus_id WHERE r.sector_from = 'KTM' AND r.sector_to = 'PKR' AND r.departure_date = '2015-12-15' 表,如下所示:

reserved_seats

"预订座位的数量"对于给定的SELECT IFNULL(SUM(s.number_of_reserved_seat),0) FROM reserved_seats s WHERE s.routine_id = '649' ,可以从bus_routine表返回,如下所示:

booking

我们可以将查询纳入"保留席位"和#34;预订座位"进入第一个查询,作为相关子查询。代替文字SELECT IFNULL(SUM(k.number_of_seat),0) FROM booking k WHERE k.bus_routine_id = '649' ,引用'649'表中的id

bus_routine

如果没有要求返回SELECT r.* , b.* -- number of booked seats , ( SELECT IFNULL(SUM(k.number_of_seat),0) FROM booking k WHERE k.bus_routine_id = r.id ) AS booked_seats -- number of reserved seats , ( SELECT IFNULL(SUM(s.number_of_reserved_seat),0) FROM reserved_seats s WHERE s.routine_id = r.id ) AS reserved_seats -- calculate available seats as -- (bus number_of_seat) - (booked_seats) - (reserved_seats) , ( b.number_of_seat - ( SELECT IFNULL(SUM(k.number_of_seat),0) FROM booking k WHERE k.bus_routine_id = r.id ) - ( SELECT IFNULL(SUM(s.number_of_reserved_seat),0) FROM reserved_seats s WHERE s.routine_id = r.id ) ) AS avaliable_seat FROM bus_routines r JOIN buses b ON b.id = r.bus_id WHERE r.sector_from = 'KTM' AND r.sector_to = 'PKR' AND r.departure_date = '2015-12-15' booked_seats列,则可以从查询中省略这些列。获取这些值的子查询只能出现在reserved_seats列的计算中。

SQL Fiddle demonstration here: http://sqlfiddle.com/#!9/64eaa/7

答案 1 :(得分:-1)