Log in

View Full Version : mySQL Order / Group by



nikomou
02-06-2009, 10:21 AM
Hello,
What i have is a simple SQL query to show all the manufacturers in my table - quite simply by using
SELECT manufacturer FROM table GROUP BY manufacturer

But what I would like is to have another query inside the one above? would that work? This one would show all the Models within the Manufacturers.

E.g.

Make 1
Make 1 product 1, Make 1 product 2, Make 1 product 3

Make 2
Make 2 product 1, Make 2 product 2, Make 2 product 2


Or is there an easier way to do this?
Thanks for your time :)

boogyman
02-06-2009, 01:20 PM
another way of doing it would be to just use two "order bys"



SELECT `manufacturer`, `product` FROM table ORDER BY `manufacturer` ASC, `product` ASC

nikomou
02-08-2009, 04:24 PM
thanks,

how would i be able to echo "<h2>Manufacture Name</h2>" when the manucature changes?

cheers

Jordi Gran
02-19-2009, 02:26 PM
In php something like this will do:

$query = "SELECT `manufacturer`, `product` FROM table ORDER BY `manufacturer` ASC, `product` ASC";
$result = mysql_query($query);

while($row = mysql_fetch_array($result))
{
if ($row[manufacturer] != $oldManufacturer)
{
$body .= '<h2>' . $row[manufacturer] . '</h2>';
}
$oldManufacturer = $row[manufacturer];
$body .= '<p>' . $row[product] . '</p>';
}

echo $body;