两个具有两个相同名称列的表的输出

时间:2014-02-11 05:11:23

标签: php mysqli

我有一个MySQL脚本,它使用INNER JOIN将两个表放在一起。两个表都有一个名为ID的字段。 我需要输出PHP,两个ID字段。我该怎么做?

这是我的剧本:

$sql = "SELECT a.id,
               a.route_id,                 
               a.requester,
               a.reservation,
               a.reservation_date,
               a.reservation_by, 
               a.telephone, 
               a.email, 
               a.firstname, 
               a.lastname, 
               a.qty_pax, 
               a.date_trip,
               a.time_trip, 
               a.trip_type, 
               a.cancelled,
               a.notes,
               a.room, 
               a.driver_id,
               b.id, 
               b.dep_symbol, 
               b.dep_location_id 
               FROM 
               general_reservations a
               INNER JOIN 
               routes b
               WHERE 
               a.cancelled<>'2' 
               AND a.date_trip BETWEEN '$find_begin' AND '$find_end' 
               AND b.dep_symbol LIKE '$code'
               AND b.id LIKE a.route_id 
               ORDER BY a.date_trip, b.dep_symbol, a.route_id";

请注意,我有两个不同的表中的a.id和b.id。

1 个答案:

答案 0 :(得分:1)

使用alias名称

$sql = "SELECT a.id AS aid,
               a.route_id,                 
               a.requester,
               a.reservation,
               a.reservation_date,
               a.reservation_by, 
               a.telephone, 
               a.email, 
               a.firstname, 
               a.lastname, 
               a.qty_pax, 
               a.date_trip,
               a.time_trip, 
               a.trip_type, 
               a.cancelled,
               a.notes,
               a.room, 
               a.driver_id,
               b.id AS bid, 
               b.dep_symbol, 
               b.dep_location_id 
               FROM 
               general_reservations a
               INNER JOIN 
               routes b
               WHERE 
               a.cancelled<>'2' 
               AND a.date_trip BETWEEN '$find_begin' AND '$find_end' 
               AND b.dep_symbol LIKE '$code'
               AND b.id LIKE a.route_id 
               ORDER BY a.date_trip, b.dep_symbol, a.route_id";