内部加入后,“where子句中的列'id'是不明确的”?

内部加入后,“where子句中的列'id'是不明确的”?

问题描述:

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).

Any idea what's wrong? Thanks

我有这个选择查询工作正常,直到我执行内连接。 p> $ 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场地 ON events.VENUE_LOCATION = venues.ID WHERE id =“。$ id)或die(mysql_error() ); code> pre>

$ id code>变量是为了使查询根据url从一行加载数据(即:page.php) ?id = 1)。 p>

知道什么是错的吗? 谢谢 p> div>

venues probably has an id field too, so you need to specify events.id, not just id, i.e.

$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());

In the WHERE clause, change id to events.id or venues.id, whichever you mean. Unfortunately, neither MySQL or I can guess which one you mean.

I guess there's a column id in table events, too? If so, the name is ambiguous, because MySQL won't know if you're talking about events.id or venues.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”. so, change your query by putting the table name with id in where clause.

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. 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.