tags:

views:

92

answers:

3
+1  Q: 

Mysql Query

Hi,

I want to fetch all the records in one line with First Name LastName , First Name Last Name, etc........ in mysql Query.

For example,

mytable looks like this:

rec Id        First Name    Last Name
1           Gnaniyar       Zubair
2           Frankyn        Albert
3           John           Mathew
4           Suhail        Ahmed

I want to fetch all these record sin one line: Output should be like this:

Gnaniyar Zubair , Frankyn Albert, John Mathew, Suhail Ahmed

How to get like this?

  • Gnaniyar Zubair
A: 

It is not a matter of getting one row with all the records, but a matter of representation of data. Therefore, I suggest to take a simple SELECT query, take the records you need, then arrange them in the view layer as you like.

On the other hand, why do you need to solve this record concatenation at SQL level and not on view level?

Cătălin Pitiș
A: 

If you wanted to get them in just one row, you're probably not using your database properly.

If you just want to join together the first and last names, that's easy:

 SELECT CONCAT(`First Name`, ' ', `Last Name`) FROM mytable
nickf
hi friends, Thanks for immediate response. this is right query. but all 4 records will display with this concatanation...i want to get all in single line.your query will give output like,Gnaniyar ZubairFrankyn Albert,John Mathew,Suhail Ahmed( total 4 records )But i want to get only one record like,Gnaniyar Zubair ,Frankyn Albert, John Mathew, Suhail AhmedThanks in advance- Gnaniyar Zubair
Gnaniyar Zubair
like I said, you're not using your database properly. thought it seems that the GROUP_CONCAT construct might help, as other have suggested.
nickf
+6  A: 

If this must the done in the query, you can use GROUP_CONCAT, but unless you're grouping by something it's a pretty silly query and the concatenation should really be done on the client.

SELECT GROUP_CONCAT(FirstName + ' ' + LastName
                    ORDER BY FirstName, LastName
                    SEPARATOR ', ') AS Names
FROM People;
Blixt
Thanks Blixt...I am getting message that 1 row fetched...but nothing is displayed except "Blob"
Gnaniyar Zubair
Indeed, it is a type of BLOB (Binary Large OBject) because it can potentially get very long. It shows up as a BLOB because your client interface takes this into consideration and hides the field.
Blixt