简体   繁体   中英

mysql result not displaying based on my query

I have a table called add_project :

+------------+---------+-------------------+--------------+
| project_id | user_id | project_status_id | project_name |
+------------+---------+-------------------+--------------+
|          1 |       1 |                 3 | abc          |
|          2 |       1 |                 0 | def          |
|          3 |       1 |                 1 | xyz          |
|          4 |       1 |                 3 | lmn          |
|          5 |       1 |                 0 | trs          |
|          6 |       1 |                 3 | ght          |
+------------+---------+-------------------+--------------+

Here on this table admin add the new projects. The admin user_id is 1 so it is all common. project_status_id indicates the completion status of that project (0=new, 1=working, 3=completed).

And I have another table called asign_project :

+----------+------------+---------+
| asign_id | project_id | user_id |
+----------+------------+---------+
|        1 |          1 |       5 |
|        2 |          5 |       9 |
|        3 |          2 |       5 |
|        4 |          4 |       5 |
|        5 |          6 |       9 |
|        6 |          3 |       9 |
+----------+------------+---------+

Here on this table the admin assigns the project to different users.

Now what I want is to find how many completed projects each user has done. As you can see, project_id(1,4) has been completed by user_id=5 .

So my question is how do I get that value?

Below is the query that I have written:

SELECT *
FROM   asign_project, add_project
WHERE  asign_project.project_id = add_project.project_id 
   AND project_status_id='3' 
   AND user_id='5'

But it is showing:

#1052 - Column 'user_id' in where clause is ambiguous

See it on sqlfiddle .

Waiting for your reply.

I guess both tables have user_id column, so you will need to more specify at table name.

Select 
    * 
FROM 
    asign_project, add_project 
WHERE 
    asign_project.project_id = add_project.project_id 
AND 
    project_status_id='3' 
AND 
    asign_project.user_id='5';

the user_id belong to what table? you should add TABLE_XXX.user_id.

  Select * FROM asign_project, add_project 
       WHERE 
          asign_project.project_id = add_project.project_id 
       AND 
          project_status_id='3' 
       AND 
          asign_project.user_id='5';

This error basically means that the two tables your select from have the same column name.

In your WHERE clause you just need to specify which tablename you want to use to get filter the data.

Example:

Select * 
FROM 
   asign_project, add_project 
WHERE 
   asign_project.project_id = add_project.project_id 
AND 
  asign_project.project_status_id='3' 
AND
  asign_project.user_id='5'

All that's been done is to replace user_id in your WHERE clause with assign_project.user_id (assuming that assign_project is the correct table you want to use)

used this

Replace user_id with add_project.user_id

Select 
  * 
FROM 
  asign_project, add_project 
WHERE 
    asign_project.project_id = add_project.project_id 
AND 
    project_status_id='3' 
AND 
    add_project.user_id='5'

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.

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