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
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.
venues probably has an id field too, so you need to specify events.id
, not just id
, ie
$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? 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.
So today I experienced this error as I was building an SQL builder class for my WordPress Plugins. 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.
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.
So once I replaced the ID with wp_post.ID or wp_users.ID, the error was gone.
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.