Welcome Guest, Not a member yet? Register   Sign In
display group_by results
#1

[eluser]Circus-Killer[/eluser]
Hi there,
I am executing a SELECT statement which uses a GROUP BY clause.
How do I print out the results in their grouped formats.
Example:

-Group 1-
value
value
value

-Group 2-
value
value
value
#2

[eluser]xwero[/eluser]
The group by in the statement compacts the group to one line so the result you get will be the last row that is fetched from the database. table:
Code:
number  name
1       me
1       I
sql statement : SELECT * FROM users GROUP BY number, outputs
Code:
number  name
1       I

You need to query the database twice, first to get the group data and then to get the group item data.

You can do it in one query if the group data is in each group item row using ORDER BY. But then your resultset can be significantly bigger and you have to do the grouping in php.




Theme © iAndrew 2016 - Forum software by © MyBB