SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot yukimushu's Avatar
    Join Date
    Feb 2005
    Location
    United Kingdom
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mixing two tables together

    Hey people,

    I'm looking at mixing both an news table, and articles table together into one query to be featured on the "Latest News and Articles" section of my website.

    Both tables are very similar, almost exact in design.

    How would I go about doing this?

    At first (off the top of my head) I thought it would go something like this?


    Code MySQL:
    SELECT news.*, articles.* FROM news, articles WHERE news.live = 1 OR articles.live=1

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    if the tables are almost exact, the query must be completely exact -- same number of columns, and same column types in respective positions

    this might work --
    Code:
    SELECT news.*
      FROM news
     WHERE news.live = 1 
    UNION ALL
    SELECT articles.* 
      FROM articles 
     WHERE articles.live = 1
    but this will surely work --
    Code:
    SELECT news.col1, news.col2, ...
      FROM news
     WHERE news.live = 1 
    UNION ALL
    SELECT articles.col1, articles.col2, ... 
      FROM articles 
     WHERE articles.live = 1
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Zealot yukimushu's Avatar
    Join Date
    Feb 2005
    Location
    United Kingdom
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers! Thats perfect!

    Works like a dream!


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
  •