SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple SELECT with PHP/MYSQL

    I have probably an easy question which keeps my mind bugging for some days now...
    I'm trying to put the following SELECT statements into 1 multi SELECT statement, PHP/MYSQL.

    SELECT * FROM myTable1
    SELECT * FROM myTable2
    SELECT * FROM myTable3 ORDER BY RAND() LIMIT 1
    SELECT row1, row2 FROM myTable4 ORDER BY id DESC LIMIT 4

    I tried UNION or UNION ALL but that doesn't work because all MYSQL tables are different in row quantity.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    use four separate queries
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Then I guess it's not possible.

    Edit: like he says ^^

  4. #4
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sometimes the mind wants it too complicated while the answer is obvious.
    Thanks for keeping my life simple!

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by PixelBoy
    SELECT * FROM myTable1
    SELECT * FROM myTable2
    As previous replies ... unless tables 1 and 2 share a common value in one or more fields which allows you to JOIN them.

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Cups View Post
    As previous replies ... unless tables 1 and 2 share a common value in one or more fields which allows you to JOIN them.
    or have similar columns which allows you to UNION them

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,023
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    As previous replies ... unless tables 1 and 2 share a common value in one or more fields which allows you to JOIN them.
    Quote Originally Posted by r937 View Post
    or have similar columns which allows you to UNION them

    Which brings us to another reason why "SELECT *" can be bad. It tells us nothing about your db structure OP, so there's not much in the way of suggestions we can make. Also, from each table, if you are building a combo box, you are only going to need 2 values anyway, so why pull all?

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    where did the 2-value combo box idea come from?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,023
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Reading too fast thinking he's talking about <SELECT> tags.


Tags for this Thread

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
  •