tags:

views:

19

answers:

1

Hello,

I have a table containing last_updated_1 and last_updated_2 columns, used respectively for text and images update time on a post.

I wish I could get a result set of 10 rows based on all time last updated records contained in the 2 columns, ex. row 1 = last_updated_1 record, row 2 = last_updated_2 record, row 3 = last_updated_1 record, etc.

How could I compare inside a MySQL query the both columns values, to get unique & mixed result set ?

Thank you in advance for your help

+1  A: 

If I understand correctly your question, this should do it:

SELECT D.val, D.tstamp
FROM
(
    (
        SELECT val AS val, last_updated_1 AS tstamp
        FROM table_1
        ORDER BY tstamp
        LIMIT 50
    )

    UNION

    (
        SELECT val AS val, last_updated_2 AS tstamp
        FROM table_2
        ORDER BY tstamp
        LIMIT 50
    )
) AS D
ORDER BY D.tstamp
LIMIT 50;
the_void
Hello and thank you for anwser. Columns are in the same table, not in 2 tables; these record update time for 1)text, 2)image updates on one same post.The script you suggest basically works but get duplicate results if difference between the 2 update values is short, and adding DISTINCT does not change a thing.I would like to select smth like (if col1 has more recent datetime than col2, than select col1 datetime value).
Kyobul
SELECT DISTINCT D.id ...FROM (( SELECT id, ...) UNION (SELECT id, ...)) AS D ...
the_void
Great ! Thank you
Kyobul
You're welcome :)
the_void