[SOLVED] Print results from two tables with no common columns ordered by date

Table of Contents

Issue

This is what I last tried.

(
SELECT  
`offers`.`id` AS `offer`, `offers`.`date` 
from `offers`
WHERE `offers`.`expired`='0'
ORDER BY `offers`.`date` DESC LIMIT 10
)
UNION ALL
(
SELECT  
`vlog`.`video`,
`vlog`.`updated`
from `vlog`
ORDER BY `vlog`.`date` DESC LIMIT 10
)

For simplicity’s sake, I have only two columns. I need to sort them by the date (not in the same results the code above provides) and show which is an offer and which is a video. Is this possible with no columns that link the tables.?

Solution

I’m assuming that video.video is an ID, since it lines up with the id column in the top query. You can select a literal string in both queries to indicate which query it came from:

SQL Fiddle

SELECT
    'offer' AS type,
    id,
    date
FROM
    offers
WHERE
    expired = 0
UNION
SELECT
    'video' AS type,
    video AS id,
    updated AS date
FROM
    vlog
ORDER BY
    date

Answered By – Travesty3

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published.