简体   繁体   中英

PHP/SQL select all with join when two tables have the same ID

I'm just getting the hang of JOINs in SQL (very powerful, could have made my code a lot more efficient if I'd looked them up earlier!), but am struggling with joining two or more tables with the same column name, then processing with PHP.

Here's the query that I've been trying, using aliases

SELECT *, TABLE1.ID AS t1_id, TABLE3.ID AS t3_id
FROM TABLE1, TABLE2, TABLE3

etc (with a left join)

Only table1 and table3 have the same ID column name, is there something wrong in this code? I'm getting the dreaded mysqli_error() in PHP!

Any help greatly appreciated - can't seem to find the solution elsewhere when selecting everything from more than one table. Could specify each column name, but there would be over one hundred!

SELECT
  t1.ID AS t1_id,
  t2.ID AS t2_id, 
  t3.ID AS t3_id
FROM
  TABLE1 as t1
LEFT JOIN TABLE2 AS t2
LEFT JOIN TABLE3 AS t3

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