PDA

View Full Version : SELECT from multiple tables with different columns


keith1995
11-24-2005, 05:51 PM
I am trying to combine 2 SELECT queries so that I can order them to display in proper date order.

The queries I am using are below. My question is, how can I join together these queries so that they will mix the news and events together to display them in order instead of displaying the news items first in date order then displaying the events beneath them in their order.

$query = "SELECT * FROM news_articles WHERE (date_full >= '$days_back' AND date_full <= '$days_ahead') AND (news_type = '$news_query') ORDER BY 'date_full' DESC";

$query = "SELECT calendar.* FROM calendar LEFT JOIN events_type ON events_type.event_id = calendar.id WHERE calendar.date_full >= '$eventdate_time' AND date_full <= '$days_time_ahead' AND STATUS = 'active' AND (events_type.type_title = '$events_query') ORDER BY 'date_full' ASC";


Anyone got any ideas?

Velox Letum
11-24-2005, 06:19 PM
(SELECT * FROM news_articles WHERE (date_full >= '$days_back' AND date_full <= '$days_ahead') AND (news_type = '$news_query') ORDER BY 'date_full' DESC)
UNION
(SELECT calendar.* FROM calendar LEFT JOIN events_type ON events_type.event_id = calendar.id WHERE calendar.date_full >= '$eventdate_time' AND date_full <= '$days_time_ahead' AND STATUS = 'active' AND (events_type.type_title = '$events_query') ORDER BY 'date_full' ASC)

Would this work for your purposes?

keith1995
11-25-2005, 05:42 AM
Velox Letum,

I tried your code but I receive the following error:

"The used SELECT statements have a different number of columns"

How can I combine the 2 queries when both tables have a different number of columns?

vinyl-junkie
11-25-2005, 09:41 PM
It would help if you posted your database schema.