anujgarg Posted September 12, 2008 Share Posted September 12, 2008 I have a table structure: id comp_name 1 a 2 a 3 a 4 b 5 b 6 a 1) Now, I am running a query which returns the company name and total number of companies with same name in group by clause ie a(4) and b(2). 2) what I want to do is, to get the individual IDs corresponding to each company name or comma separated ie a (4) - 1,2,3,6 and b(2) - 4,5 I have covered the step 1) but how do I cover the step 2). All I am getting is the same IDs in both cases: ie a(4) - 1,2,3,4,5,6 b(2) - 1,2,3,4,5,6 How to solve this problem? TIA Link to comment https://forums.phpfreaks.com/topic/123967-group-by-problem/ Share on other sites More sharing options...
Mchl Posted September 12, 2008 Share Posted September 12, 2008 Showing us how you're getting these result would help. Link to comment https://forums.phpfreaks.com/topic/123967-group-by-problem/#findComment-639930 Share on other sites More sharing options...
PFMaBiSmAd Posted September 12, 2008 Share Posted September 12, 2008 Use the mysql GROUP_CONCAT() function. Link to comment https://forums.phpfreaks.com/topic/123967-group-by-problem/#findComment-639931 Share on other sites More sharing options...
sasa Posted September 12, 2008 Share Posted September 12, 2008 select comp_name, COUNT(id) as num, GROUP_CONCAT(id) as ids FROM table_name GROUP BY name Link to comment https://forums.phpfreaks.com/topic/123967-group-by-problem/#findComment-639945 Share on other sites More sharing options...
anujgarg Posted September 13, 2008 Author Share Posted September 13, 2008 sasa, I applied your query and I found the following: company_name num ids 2 [bLOB - 5 B] grger 1 [bLOB - 2 B] xgfdg 3 [bLOB - 8 B] xgfdgdfdfdf 1 [bLOB - 2 B] how do i get the numeric values in ids.... Link to comment https://forums.phpfreaks.com/topic/123967-group-by-problem/#findComment-640320 Share on other sites More sharing options...
Stryves Posted September 13, 2008 Share Posted September 13, 2008 Just add it to the query. select comp_name, id, COUNT(id) as num, GROUP_CONCAT(id) as ids FROM table_name GROUP BY name Link to comment https://forums.phpfreaks.com/topic/123967-group-by-problem/#findComment-640328 Share on other sites More sharing options...
anujgarg Posted September 13, 2008 Author Share Posted September 13, 2008 Stryves This is also not what I need. I give you the statements what I am writing: SELECT *, count(*) as count FROM table GROUP BY comp_name foreach ($agentList->result() as $search) { echo "<tr><td><strong>".$search->id . $search->comp_name. search->count</strong></td></tr>"; echo "<tr><td> </td></tr>"; } SELECT id FROM table foreach ($agentId->result() as $searchId) { $jobId .= $searchId->jobs_id . ","; } The first query returns a(4) and b(2) as per my example. The second query returns 1,2,3,4,5,6 in both cases which I need with their corresponding names and Ids. Link to comment https://forums.phpfreaks.com/topic/123967-group-by-problem/#findComment-640335 Share on other sites More sharing options...
sasa Posted September 13, 2008 Share Posted September 13, 2008 select comp_name, COUNT(id) as num, CAST(GROUP_CONCAT(id) AS CHAR) as ids FROM table_name GROUP BY name Link to comment https://forums.phpfreaks.com/topic/123967-group-by-problem/#findComment-640348 Share on other sites More sharing options...
anujgarg Posted September 13, 2008 Author Share Posted September 13, 2008 nice sasa, thanks a lot... It worked Link to comment https://forums.phpfreaks.com/topic/123967-group-by-problem/#findComment-640444 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.