Results 1 to 3 of 3

Thread: One query table

  1. #1
    Join Date
    Apr 2006
    Location
    Amsterdam
    Posts
    350
    Thanks
    20
    Thanked 11 Times in 11 Posts

    Default One query table

    Guys,

    It possible to do all this with just one SQL query?

    Let’s say I have a table with health info on all my pets in a MySQL database.

    Code:
    +--------+---------+--------+
    | name   | specie  | health |
    +--------+---------+--------+
    | Bo Bo  | bird    | good   |
    | Diane  | cat     | good   |
    | Bells  | Rabbit  | good   |
    | Lizie  | cat     | good   |
    | Benny  | dog     | ill    |
    | Gwen   | hamster | good   |
    | Roger  | Rabbit  | good   |
    | Harold | dog     | good   |
    | Smoky  | bird    | good   |
    | Clio   | Rabbit  | good   |
    +--------+---------+--------+
    I am trying to create a page to show all the pets info on. The pet info are ordered and grouped by animal species in a HTML table with a header showing the specie and a health summary. If all goes well it should look something like this:

    Code:
    +--------+--------+
    |  birds      ok  |     <----- header, shows specie and health summary, all birds are fine so the health summary is: ok
    +--------+--------+
    | Smoky  | good   |
    | Smoky  | good   |
    | Bo Bo  | good   |
    +--------+--------+
    |  cats       ok  |
    +--------+--------+
    | Diane  | good   |
    | Lizie  | good   |
    +--------+--------+
    |  dogs   -=NOK=- |     <----- One poor dog is ill so the health summary should return: nok
    +--------+--------+
    | Harold || good  |
    | Benny  |  ill   |
    +--------+--------+
    |  hamster    ok  |
    +--------+--------+
    | Gwen   | good   |
    +--------+--------+
    |  rabbits    ok  |
    +--------+--------+
    | Roger  | good   |
    | Bells  | good   |
    | Clio   | good   |
    +--------+--------+
    I am close to having all of this in my script using just one SQL query. I only heaving trouble with the health summary in the header of the table. Is there a way summarize the health of a group of species?

    This my script so far: (its in ASP, any pointer of solution in PHP is just as usefull)
    Code:
    Set RS = objDBConn.Execute("SELECT name,specie,health FROM `pets` ORDER BY specie, name ASC")
    while not RS.eof
     If strServerType <> RS("specie") Then
      i=0
     End if   
     
     If i = 0 Then
       Response.write(RS("specie") & "health summary: ? " & vbCrLf) 
     End If
     
       Response.write(RS("name") & " - " RS("health") & vbCrLf) 
     
    i = i + 1
    strServerType = RS("specie") 
    RS.MoveNext
    wend
    I think I am close, i've got everything but the health summary.

  2. #2
    Join Date
    Apr 2005
    Location
    Haslemere, Surrey, UK
    Posts
    338
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default

    Try this code...

    Code:
    SELECT 
    1 as header, 
    specie,
    name,
    health 
    FROM animals 
    UNION ALL 
    SELECT 
    0 as header, 
    specie,
    specie,
    CASE WHEN MAX(health) = 'ill' THEN '-=NOK=-' ELSE 'OK' END
    FROM animals 
    GROUP BY specie
    ORDER BY specie, header, name ASC
    Last edited by Warren Ashcroft; 17th March 2008 at 03:10 PM.
    Alastair - WOWD



  3. The Following User Says Thank You to Wise Webs For This Useful Post:

    RFH Reseller: Rappie (17th March 2008)

  4. #3
    Join Date
    Apr 2006
    Location
    Amsterdam
    Posts
    350
    Thanks
    20
    Thanked 11 Times in 11 Posts

    Default

    Cheers, works flawlessly!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Multiple Table Query
    By spire128 in forum MSSQL
    Replies: 6
    Last Post: 20th October 2006, 05:21 PM
  2. Embed Pivot Table
    By carlosgilf in forum ASP.NET
    Replies: 0
    Last Post: 8th September 2006, 04:47 PM
  3. Table-less design, css & XHTML
    By JohnnyW in forum Website and Graphic Design
    Replies: 2
    Last Post: 22nd December 2005, 01:06 PM
  4. Table Corner Rounding
    By Breaks in forum Development Support
    Replies: 8
    Last Post: 2nd November 2005, 10:21 PM

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
  •