esender Posted March 6, 2009 Share Posted March 6, 2009 I'd like to query my database such that it returns a single cell that has all my results in one Comma delimited string. here is an example. Normally, someone says: SELECT title FROM node WHERE type = 1 LIMIT 20 This will return 20 separate rows: "Title1" "Title2" ... "Title20" Well, how do I query it so that it actually returns a delimited, single row, single coloum: "Title1,Title2,Title3,Title4,Title5,Title6,...,Title20" All in one row, one column? Quote Link to comment https://forums.phpfreaks.com/topic/148290-query-database-to-return-a-single-rowcolumn-with-comma-delimited-string/ Share on other sites More sharing options...
Maq Posted March 6, 2009 Share Posted March 6, 2009 SELECT GROUP_CONCAT(title) FROM node WHERE type=1 LIMIT 20 Quote Link to comment https://forums.phpfreaks.com/topic/148290-query-database-to-return-a-single-rowcolumn-with-comma-delimited-string/#findComment-778518 Share on other sites More sharing options...
esender Posted March 10, 2009 Author Share Posted March 10, 2009 SELECT GROUP_CONCAT(title) FROM node WHERE type=1 LIMIT 20 Sweet thanks! Quote Link to comment https://forums.phpfreaks.com/topic/148290-query-database-to-return-a-single-rowcolumn-with-comma-delimited-string/#findComment-780918 Share on other sites More sharing options...
aschk Posted March 10, 2009 Share Posted March 10, 2009 Curiously why would you want this? You're better of keeping it as a rowed dataset, then you can manipulate any rendered output in your scripting language instead. Quote Link to comment https://forums.phpfreaks.com/topic/148290-query-database-to-return-a-single-rowcolumn-with-comma-delimited-string/#findComment-780993 Share on other sites More sharing options...
esender Posted March 10, 2009 Author Share Posted March 10, 2009 Curiously why would you want this? You're better of keeping it as a rowed dataset, then you can manipulate any rendered output in your scripting language instead. I do this because the way my table is layed out, a single node ID can correspond to multiple titles. Say, an Item is rated "best" in multiple categories. Quote Link to comment https://forums.phpfreaks.com/topic/148290-query-database-to-return-a-single-rowcolumn-with-comma-delimited-string/#findComment-780999 Share on other sites More sharing options...
Maq Posted March 10, 2009 Share Posted March 10, 2009 Curiously why would you want this? You're better of keeping it as a rowed dataset, then you can manipulate any rendered output in your scripting language instead. I do this because the way my table is layed out, a single node ID can correspond to multiple titles. Say, an Item is rated "best" in multiple categories. You shouldn't keep comma delimited strings in your DB. If you have a node with multiple titles then you should have multiple records for that node. Quote Link to comment https://forums.phpfreaks.com/topic/148290-query-database-to-return-a-single-rowcolumn-with-comma-delimited-string/#findComment-781103 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.