Hey i have a query ordered by date, and i want in case the date is identical to order by time added to maintain stable results.
I have the following query :
(
SELECT 'album' AS RowType, id, DATE, title, time_added
FROM albums
WHERE is_temp =0
AND subject_id = '3'
)
UNION ALL (
SELECT 'video' AS RowType, id, DATE, title, time_added
FROM videos
WHERE is_temp =0
AND subject_id = '3'
)
UNION ALL (
SELECT 'story' AS RowType, id, DATE, title, time_added
FROM stories
WHERE is_temp =0
AND subject_id = '3'
)
ORDER BY DATE, time_added
Which works perfectly but i don’t want to select the time_added column as i don’t need it in my result set.
Can i somehow achive this? if i remove the time_added the query won’t run.
I have found the following :
The ORDER BY clause causes the output rows to be sorted.
The argument to ORDER BY is a list of expressions that are used as the
key for the sort. The expressions do not have to be part of the result
for a simple SELECT, but in a compound SELECT each sort expression
must exactly match one of the result columns. Each sort expression may
be optionally followed by a COLLATE keyword and the name of a
collating function used for ordering text and/or keywords ASC or DESC
to specify the sort order.
Which i can’t decide if means it’s impossible or i should try harder.
try this