Hi! I am getting message #1052 - Column ‘userlevel’ in where clause is ambiguous while running following query:
<?PHP
$query=mysql_query(“SELECT mssgs.start_date, mssgs.task, mssgs.status, mssgs.timestamp, mssgs.due_on, mssgs.id, users.fname FROM mssgs
INNER JOIN users ON mssgs.uid=users.uid AND mssgs.userlevel=users.userlevel WHERE status=0 AND userlevel=1 OR userlevel=3”);
if(mysql_num_rows($query) > 0)
{
You have a conflict with a duplicate row title, I’m guessing its “id”.
mssgs.id and users.uid
So you should do
SELECT mssgs.start_date, mssgs.task, mssgs.status, mssgs.timestamp, mssgs.due_on, mssgs.id, users.uid AS userID, users.fname FROM mssgs
INNER JOIN users ON mssgs.uid=userID AND mssgs.userlevel=users.userlevel WHERE status=0 AND userlevel=1 OR userlevel=3
Or something along those lines, wherever a conflict is happening
It’s a conflict between two column names. As the message says, you are using more than 1 table in your query that contain a column named ‘userlevel’.
Here you’re handling that situation correctly
Thanks a lot for your valuable replies.
I think I need to oil my skills for dealing with joins!!
The problem is resolved for me now but the new problem is, if I run the following query:
"SELECT task.task_id, task.task_to, task.task_from, task.timestamp, task.due_on, task.status, task.task, user.fname FROM task
INNER JOIN user ON task.userlevel=user.userlevel WHERE task.status=1"
I dont get output even when there is data in the database which has status 1.
Simply no data found message is displayed.
Please try to write your queries in a readable format (you don’t write all your code on a single line, do you? )
SELECT
task.task_id
, task.task_to
, task.task_from
, task.timestamp
, task.due_on
, task.status
, task.task
, user.fname
FROM task
INNER JOIN user
ON task.userlevel = user.userlevel
WHERE task.status = 1
there is data in the database which has status 1.
There is data in the ‘task’ table that has status 1 you wanted to say, right?
But do those rows have a userlevel that is present in the ‘user’ table?
In this query you are using an INNER JOIN, and in the ON clause you are specifying how the two tables must be joined: those rows from ‘task’ and ‘user’ that have the same value in the userleve fields are joined. All others are excluded.
For example:
‘taks’ table
task_id userlevel
1 10
2 20
3 30
4 10
‘user’ table
fname userlevel
Tom 10
Jane 30
Sue 40
Joining these two table on userlevel (lik you do in your query), will give the following result:
task.task_id task.userlevel user.fname user.userlevel
1 10 Tom 10
3 30 Jane 30
4 10 Tom 10
Task 2 is excluded, because there’s no row in ‘user’ with userlevel 20.
User Sue is excluded, because there’s no row in ‘task’ with userlevel 40.