如何左联接两个具有相同列名的表 [英] How to LEFT JOIN two tables with the same column name

查看:106
本文介绍了如何左联接两个具有相同列名的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想左联接两个具有相同列名的表.我有两个要加入的表,但我不断收到错误消息:

I want to LEFT JOIN two tables with the same column name. I have two tables which I am trying to join but I keep getting an error:

column 'id' is ambiguous

在我的JSON输出中返回.

returned in my JSON output.

我当前的查询

$sQuery = "
 SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
 FROM   $sTable
 LEFT JOIN
    $sTable2
    ON ($sTable2.id = $sTable.id)

 $sWhere
 $sOrder
 $sLimit
";

产生该错误.每个表中都有相同的列名时,如何将这两个表作为连接点连接?

Produces that error. How can I join these two tables as the join point when there is the same column name in each table?

推荐答案

明确说明该列属于哪个表.这也适用于查询的SELECT部分​​:

Be explicit about which table the column belongs to. This also applies to the SELECT part of the query:

SELECT table1.column AS column1, table2.column AS column2
FROM table1
LEFT JOIN table2
ON table1.column = table2.column

要节省一些键入时间,请使用表别名:

To save you some typing time, use table aliases:

SELECT t1.column AS column1, t2.column AS column2
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.column = t2.column

这篇关于如何左联接两个具有相同列名的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆