SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot pozmu's Avatar
    Join Date
    Jul 2001
    Location
    Poland/ Warsaw
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql - selecting from two tables in one query

    One question: how can I make this:
    SELECT * FROM catone order by id desc limit 5

    SELECT * FROM cattwo order by id desc limit 5

    in single query?

  2. #2
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT * FROM catone,cattwo order by id desc limit 5

    theres a small chance that's wrong, cause that's how it's done in sql, dunno bout MySQL.
    but try it. probably'll work
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  3. #3
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $yaks=mysql_query("SELECT * FROM catone,cattwo ORDER BY catone.id DESC");

    while($row=mysql_fetch_assoc($yaks)){
    while(list($key,$var)=each($row)){echo "$key , $var<br>";}}

    should do the job.


    DOH - Defender beat me to it !

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  5. #5
    SitePoint Zealot pozmu's Avatar
    Join Date
    Jul 2001
    Location
    Poland/ Warsaw
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm, I was trying a query like this before (I use fetch_array), but this doesn't work - I get 10 (I added LIMIT 10) items but only from the first table.
    One more time:
    I have two tables. I want to get 5 newest items from each table in single mysql query. I know that is easy - I'm sure that I do query like this before, but... I don't remember how .


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
  •