+ Reply to Thread
Results 1 to 5 of 5

Thread: query multiple tables and present data depending on the table

  1. #1
    wjh2303 is offline x10 Sophmore wjh2303 is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    139

    query multiple tables and present data depending on the table

    I have data (date-stamped) in multiple tables that i would like to present ordered by time. However how the data needs to be presented depends on which table it is from. How can i check which table some data in a union is from and present it accoringly, i.e:
    Code:
    mysql_query( "(SELECT columns FROM tableA) UNION (SELECT columns FROM tableB) ORDER BY timestamp DESC;
    
    for(all rows){
       if(row is from tableA){
            format like A
       }
       else{
            format like B
        }
    }

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,267

    Re: query multiple tables and present data depending on the table

    My quick and lazy way would be to add a column to each table, call it table_name. Default value 'A' for table A, 'B' for table B and request table_name as part of the UNION. You have your flag.

    Let me about on a more elegant way to do it.
    Nothing is always absolutely so.

  3. #3
    wjh2303 is offline x10 Sophmore wjh2303 is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    139

    Re: query multiple tables and present data depending on the table

    That option did come to mind, but i was hoping someone could point me to an alternative, more elegant (as you phrase it) method. If no-one else provides any input then i shall do that

    Thanks
    Last edited by wjh2303; 03-13-2010 at 05:12 PM.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,267

    Re: query multiple tables and present data depending on the table

    Pretty sure you can do it without adding columns.


    PHP Code:
    $q =  "(SELECT 1 as flag, columns FROM tableA) UNION (SELECT 2 as flag, columns FROM tableB) ORDER BY timestamp DESC;" 
    then the first column will be your flag.
    Nothing is always absolutely so.

  5. #5
    wjh2303 is offline x10 Sophmore wjh2303 is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    139

    Re: query multiple tables and present data depending on the table

    excellent, thats worked perfectly, thanks

+ Reply to Thread

Similar Threads

  1. View sql data in html tables
    By azamkandy in forum Free Hosting
    Replies: 8
    Last Post: 10-13-2009, 12:34 PM
  2. MysQL Query for Master table & child table
    By phpasks in forum Programming Help
    Replies: 8
    Last Post: 08-07-2008, 09:07 AM
  3. tables in database table entries
    By stalkio in forum Programming Help
    Replies: 3
    Last Post: 07-26-2008, 08:22 PM
  4. CGI search form - Searching multiple tables via dropdown
    By dwd2000 in forum Programming Help
    Replies: 0
    Last Post: 02-19-2008, 02:56 PM
  5. MYSQL databases present but no data in them
    By Brokenfor in forum Free Hosting
    Replies: 2
    Last Post: 01-18-2008, 12:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers