简体   繁体   中英

Output from two tables that has two same name columns

I do have a MySQL script that put together two tables using INNER JOIN. Both tables, have a field called ID. I do need to output with PHP, both ID fields. How do I do this?

This is my script:

$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";

Notice that I do have a.id and b.id from two different tables.

Use an alias name,

$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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM