Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grouping resoults

Status
Not open for further replies.
Aug 28, 2005
6
HR
Hello,

I have an query that selects some records from a database what need to be grouped by an ID value.

I can have multiple records with the same ID and they need to be grouped by it, an example:

an array from query would be:
1. 1 record one
2. 1 record two
3. 8 record one
4. 8 record two
5. 8 record three

and the final output that I need is

1
-------
record one
record two

8
--------
record one
record two
record three


I would appriciate if anyone would help me to pull this off.
 
Hm, I can use DISTINCT to get the number of diferent records (by that ID that can be the same)...

$num_ppl = mysql_num_rows(mysql_query("SELECT DISTINCT user_id FROM mytable WHERE invalid != '1'"));

while ($i < $num_ppl) {
echo "what now :(";
}
 
when you say "grouping" you really mean sequencing

for this, use ORDER BY

as for the fancy indentation/formatting (i.e. print 1 once, then record one, record two, then print 8 once, then record one, record two, record three) this is a presentation layer consideration and should be handled in the application code, not the sql

r937.com | rudy.ca
 
You are right, I got confused because of the mysql GROUP modifier, this is acctually quite simple to write in php.

Thanks for your time,

Milan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top