Jump to content

[SOLVED] Grabbing from 2 tables duplicates the rows about 10 times


envexlabs

Recommended Posts

Hello,

 

Basically, i have 2 tables. events and specials.

 

These are 2 separate tables with no linking elements. I want to grab from both because i want to display a list of the 30 newest events and specials created, sorted by date.

 

Here is the table structures:

 

SPECIALS
-------------------------------
id
venue_id
title
more_info
created

EVENT
-------------------------------
id
venue_id
fb_eid
title
date
poster_thumb
poster_large
buy_link
more_info

 

The only thing these two tables share is the venue_id.

 

Here is the query:

SELECT * FROM events, specials 
WHERE events.venue_id 
IN (1,20)
AND specials.venue_id
IN (1,20)

 

I'm obviously not grasping this concept, or doing something completely wrong, but i can't seem to figure out what.

 

Can anyone shed any light on this subject?

 

Thanks!

Link to comment
Share on other sites

So what you want to do is a Union.  In order to get a union to work the result sets have to be the same on both queries.  Depending on what columns you need from both tables you may need to manufacture columns in your select statement. 

 

The main trick here,  is that you want to ORDER BY the date in question, but it seems that the two tables have different names for that column: one is 'created', and the other is 'date'.  So you'll need to alias the columns so that they have the same name in the union.

 

So something like this:

 

SELECT title, created FROM specials
UNION
SELECT title, date as created FROM events
ORDER BY created DESC
LIMIT 0, 30

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.