简体   繁体   English

内部联接后,“ where子句中的列'id'不明确”?

[英]“Column 'id' in where clause is ambiguous” after inner joining?

I had this select query which worked fine until I performed an inner join. 我有这个选择查询,在我执行内部联接之前,它运行良好。

$id = (int) $_GET['id'];
$data = mysql_query("
SELECT 
    events.start_datetime, 
    events.EVENT_NAME,
    events.START_TIME,
    events.END_TIME, 
    events.VENUE_LOCATION,
    venues.VENUE_NAME
FROM 
    events 
INNER JOIN venues 
        ON events.VENUE_LOCATION = venues.ID 
WHERE 
    id = ".$id) or die(mysql_error());

The $id variable is so that the query loads data from a row depending on the url (ie: page.php?id=1). $id变量使查询根据行从URL加载数据(即:page.php?id = 1)。

Any idea what's wrong? 知道有什么问题吗? Thanks 谢谢

In the WHERE clause, change id to events.id or venues.id , whichever you mean. 在WHERE子句中,更改idevents.idvenues.id ,无论你的意思。 Unfortunately, neither MySQL or I can guess which one you mean. 不幸的是,无论是MySQL还是我都猜不到您的意思。

venues probably has an id field too, so you need to specify events.id , not just id , ie 场所可能也有一个id字段,因此您需要指定events.id ,而不仅仅是id ,即

$id = (int) $_GET['id'];
$data = mysql_query("
SELECT 
    events.start_datetime, 
    events.EVENT_NAME,
    events.START_TIME,
    events.END_TIME, 
    events.VENUE_LOCATION,
    venues.VENUE_NAME
FROM 
    events 
INNER JOIN venues 
        ON events.VENUE_LOCATION = venues.ID 
WHERE 
    events.id = ".$id) or die(mysql_error());

I guess there's a column id in table events , too? 我猜在表events中也有列id If so, the name is ambiguous, because MySQL won't know if you're talking about events.id or venues.id . 如果是这样,这个名字是模糊的,因为MySQL不知道,如果你在谈论events.idvenues.id It doesn't matter you didn't name it as one of the columns to be selected. 没有将其命名为要选择的列之一也没关系。 It exists in more than one table, so you have to tell it which one you actually want. 它存在于多个表中,因此您必须告诉它实际上想要哪个表。

You did not specify for query to use id from which table, and both tables have column name "id" so, it throws the error of “Column 'id' in where clause is ambiguous”. 您没有指定查询使用哪个表中的id,并且两个表的列名称都为“ id”,因此,它将引发“ where子句不明确的列'id'”错误。 so, change your query by putting the table name with id in where clause. 因此,通过将带有id的表名放在where子句中来更改查询。

Put either WHERE events.id = ... or WHERE venues.id = ...

If a column name is not unique to a single table, then it must be qualified with the table name (or the table alias if appropriate). 如果列名不是单个表唯一的,则必须用表名(或适当的表别名)来限定它。 So, apparently id appears in both tables. 因此,显然id出现在两个表中。 From the description, it sounds as if venues was the added table, so (guessing here) you probably need to qualify it as events.id in the WHERE clause. 从描述中,听起来好像是添加了表格的venues ,所以(在这里猜测)您可能需要将其限定为WHERE子句中的events.id

So today I experienced this error as I was building an SQL builder class for my WordPress Plugins. 所以今天我在为WordPress插件构建SQL构建器类时遇到了此错误。 As I tried to execute queries like this; 当我尝试执行这样的查询时;

SELECT ID,post_author,user_login,post_title,user_login,user_email FROM wp_users INNER JOIN wp_posts USING (post_title)

I was getting errors about ID been ambiguous so I made some research and found out the problem. 我发现有关ID的错误模棱两可,因此我进行了一些研究并找出了问题所在。

The two tables I was trying to join all have a column called ID, and since I did not specify the particular column, MYSQL was confused on which table I want to select from. 我试图连接的两个表都有一个名为ID的列,并且由于我没有指定特定的列,因此MYSQL对于我要从哪个表中进行选择感到困惑。

So once I replaced the ID with wp_post.ID or wp_users.ID, the error was gone. 因此,一旦我用wp_post.ID或wp_users.ID替换了ID,错误就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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