[英]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. 我刚刚掌握了SQL中的JOIN(非常强大,如果我早先看过它们,可能会使我的代码更高效!),但是却在连接两个或更多个具有相同列名的表时遇到了麻烦,然后用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? 只有
table1
和table3
具有相同的ID列名称,此代码有问题吗? I'm getting the dreaded mysqli_error()
in PHP! 我在PHP中遇到了可怕的
mysqli_error()
!
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.