Click here to Skip to main content
16,015,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Good Evening

My doubt is i have a table called userinfo in my database with one of the column as SenderId.Now i want to get all the values of the SenderId column into a string type array using comma seperator.Can you please tell me how to write the query for this in mysql.

Thanks in advance
Posted

SQL
DECLARE @Output varchar(max) = ''
SELECT @Output = coalesce(@Output+',','') + CONVERT(varchar(max),Col1) FROM Table1
SET @Output = RIGHT(@Output,LEN(@Output)-1)
PRINT @Output
 
Share this answer
 
Comments
Manas Bhardwaj 18-Jul-11 7:02am    
Correct, but this is more T-Sql way of doing. In MySQL, you can do it with GROUP_CONCAT function. See my answer.
Anyways, +5. :)
[no name] 18-Jul-11 7:41am    
Thanks :)
like this:

SELECT GROUP_CONCAT(SenderID) FROM UserInfo
 
Share this answer
 
Comments
OriginalGriff 18-Jul-11 7:08am    
Nice solution - simple and effective. :thumbsup:
Manas Bhardwaj 18-Jul-11 7:09am    
thanks :)
komali Guntur 18-Jul-11 7:19am    
Thank You Bhardwaj with this query i am getting the comma seperated values when i execute the query through the query browser in mysql.But when i kept the same query in my code i am getting the exception as org.hibernate.MappingException: No Dialect mapping for JDBC type: -1. My code is like this

session = HibernateSessionFactory.getSession();

String hql = "select GROUP_CONCAT(SenderId) FROM userinfo";

senderIdValue = (String)session.createSQLQuery(hql).list().get(0);

Can you give me any idea about this exception.

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