Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    one result by two tables

    hello coding forums members! im using php+mysql.

    maybe someone have an idea how to make 1 array by is results from 2 tables which are ordered?

    my current idea

    Code:
    while(..SELECT * FROM audi ORDER BY date DESC)
    $r[] = somedata;
    while(..SELECT * FROM bmw ORDER BY date DESC)
    $r[] = somedata;

  • #2
    New to the CF scene
    Join Date
    May 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i dont see a edit button so i reply again
    ----------------------------------------------

    maybe someone have an idea how to make 1 array by results from 2 tables which are ordered?

    my current idea, but i need them to get on $r[] finally ordered results by date

    Code:

    while(..SELECT * FROM audi ORDER BY date DESC)
    $r[] = somedata;
    while(..SELECT * FROM bmw ORDER BY date DESC)
    $r[] = somedata;

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    *PROBABLY* what you want is a UNION:
    Code:
    SELECT * FROM audi
    UNION ALL
    SELECT * FROM bmw
    ORDER BY `date` DESC
    BUT...

    But to do that *ALL* the fields in the two tables must be in the same order and have the same data type.

    If that's not the case, then you need to select the fields individually:
    Code:
    SELECT number1, text2, text3, date4 FROM audi
    UNION ALL
    SELECT numberA, textB, textC, dateD FROM bmw
    ORDER BY date4 DESC
    Notice that you use only ONE ORDER BY. it will mix the tables and put the results in order by date.

    I also need to comment: If your two tables really are identical in structure (same fields, same types, etc.), then more than likely you made a mistake in your database design and you should have had only one common table, not two. Having two identical tables is usually a sign of a beginner's mistake.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    mrbemvee (05-12-2013)


  •  

    Posting Permissions

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