Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

How to show the member's name form table
My Problem:
I have a table name userDetails
Columns of userDetails are
1.pk_id
2.First_name
3.Last_name
etc.

i want to find the user's first name for more than on pk_id
I have tried
SQL
SELECT FIRST_NAME FROM USERDETAILS WHERE PK_ID = (SELECT GROUP_CONCAT(USER_ID) FROM PROJECTS WHERE PROJECT_ID = 2 GROUP BY PROJECT_ID);

This is showing only first name of first pk_id, but i want to show all the first names of the userids that subquery will return.
means:
If the subquery will return 2,3,4,5 than i want to show the first_name of pk_id=2,pk_id=3,pk_id=4,pk_id=5
How to write a query so that it will return all first names of the userids selected by the subquery

Please Help

Thanks
Posted
Updated 18-Jun-12 4:15am
v4

1 solution

The syntax you have listed is correct. I ran a test (in Microsoft SQL but there should not be a difference in this case) and the results were that two rows were returned. Are you only looking at the first row returned? Otherwise, there must be something wrong with your data because the query is right.

Moving forward, check to be sure you are looking at every row (not just the first). Next, check to be sure you have both an PK_ID=2 and PK_ID=3 in your table (maybe the 3 row is missing?). Finally, look at your database itself. There might be a corrupted index that is returning bad data (unlikely but possible).

Update:
Based upon your updated information, you need to change your query to use the IN statement instead of the equals statement and you need to take off the GROUP_CONCAT statement like so:
SQL
SELECT FIRST_NAME FROM USERDETAILS WHERE PK_ID IN (SELECT USER_ID FROM PROJECTS WHERE PROJECT_ID=2);

The one thing you need to watch out for is that the sub-query returns one column of data (the USER_ID column). That will be converted by SQL into something similar to your first query (in essence). If you return multiple rows, it will compare the value for each row against the PK_ID to see if it matches. If it does, it will include the row from the main query because of the match.
 
Share this answer
 
v4
Comments
bhagirathimfs 18-Jun-12 9:30am    
Actually pk_id's are coming from different query.So i can't do pk_id =2 and pk_id=3 .
please give me a solution which will do the above task.
SELECT FIRST_NAME FROM USERDETAILS WHERE PK_ID = (SELECT GROUP_CONCAT(USER_ID) FROM PROJECTS GROUP BY PROJECT_ID);

If the subquery return one row than the qbove query will run fine. but if it will return multiple rows than error will occur.

i want to write query for multiple user_id (So i have used "IN" on the above query but it is showing and it should show first row value).Please help
Tim Corey 18-Jun-12 9:48am    
That really changes your question quite a bit. Please use the "Improve Question" link to modify what you are asking for. In the meantime, I'll work on updating my solution.
bhagirathimfs 18-Jun-12 9:56am    
Improved the question please post your solution.
Tim Corey 18-Jun-12 9:58am    
I updated my solution. Basically, you need to use the IN statement and you don't need to do the GROUP_CONCAT statement. You want one row per ID returned in the sub-query.
bhagirathimfs 18-Jun-12 10:10am    
I think if we don't use group_concat than it will return one user id(first one) ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900