If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Join/Union Two Tables (Articles and Videos)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-30-10, 23:36
tlshaheen tlshaheen is offline
Registered User
 
Join Date: Dec 2009
Posts: 20
Join/Union Two Tables (Articles and Videos)

I've got two tables, "videos" and "articles". I want to list the most recently added ones on my frontpage, for example. I want to join the two tables together, and then display the most recent ones using ORDER BY creation_date DESC from the new combined list.

"videos":
video_id, youtube_id, video_title, video_intro, video_content, video_picture, author_id, creation_date, last_modified_date, frontpage, video_path

"articles":
article_id, article_title, article_intro, article_content, article_picture, author_id, creation_date, last_modified_date, frontpage, article_path

I need to join these two tables together so that for example, article_title and video_title become "title".

Is there any way to do this, or must article_title and video_title (for example) be named the same in the table?

Thanks

P.S.
I've tried the following, but i know its not even close to right

SELECT article_id, article_title as title, article_intro, article_content, article_picture, a.author_id, v.author_id, date_format(a.creation_date, '%M %D, %Y') AS formatted_creation_date, date_format(v.creation_date, '%M %D, %Y') AS formatted_creation_date, article_path, video_title as title, video_intro, video_content, video_picture, video_path, a.creation_date as create_date, v.creation_date as create_date
FROM articles as a, videos as v
ORDER BY creation_date DESC
Reply With Quote
  #2 (permalink)  
Old 03-31-10, 06:37
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Code:
select article_title as title
        ,other article columns...
        ,creation_date
   from articles
union
select video_title as title
        ,other video columns...
        ,creation_date
   from videos
order by creation_date
As an aside, since the tables are nearly identical, why do you have separate tables? You could have included a type column which would define video/article. For that matter, you could include types such as (e)book, (e)magazine, picture, etc...
Dave
Reply With Quote
  #3 (permalink)  
Old 03-31-10, 08:34
tlshaheen tlshaheen is offline
Registered User
 
Join Date: Dec 2009
Posts: 20
I will try out that select statement, thanks!

Quote:
Originally Posted by dav1mo View Post
As an aside, since the tables are nearly identical, why do you have separate tables? You could have included a type column which would define video/article. For that matter, you could include types such as (e)book, (e)magazine, picture, etc...
Dave
There is a one key difference between the tables: videos has a "youtube_id" column, which articles would never fill. Would it still be worth it to combine the tables, even though youtube_id would be blank for hundreds of rows?
Reply With Quote
  #4 (permalink)  
Old 03-31-10, 11:15
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
If were my system I would not care.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On