Results 1 to 4 of 4

Thread: mySQL Order / Group by

  1. #1
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default mySQL Order / Group by

    Hello,
    What i have is a simple SQL query to show all the manufacturers in my table - quite simply by using
    HTML Code:
    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

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    another way of doing it would be to just use two "order bys"

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

  3. #3
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    thanks,

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

    cheers

  4. #4
    Join Date
    Feb 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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;

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •