tags:

views:

87

answers:

4

Hi all,

I'am trying to understand what causes the following, maybe you could help me:

I have a query like:

 select field1,fieldDate from table1 
 union all 
 select field1,fieldDate from table2
 order by fieldDate desc

and the another one like this:

 select field1,field2,fieldDate from table1 
 union all 
 select field1,field2,fieldDate from table2 
 order by fieldDate desc

So basically they are the same with the exception that in the second I retrieve an extra field.

Now, both results come with a diferent ordering, but just for the cases that the dates are exacly the same. For example there are 2 rows (row1,row2) with date 2009-11-25 09:41:55. For query 1 row1 comes before row2 and for query 2 row2 comes before row1. Does somebody knows why this happens?

Thanks, Regards

A: 

EDIT: This answer is wrong: the order by works on the entire union. I'll leave it here to save others the trouble :)


Your order by only works on the second part of the union. You can use a subquery to make the order by work on the entire union:

select field1,field2,fieldDate 
from (
    select field1,field2,fieldDate 
    from table1 
    union all 
    select field1,field2,fieldDate 
    from table2
) SubQueryName
order by fieldDate desc
Andomar
I see that I have a problem with the query structure, thanks. Anyway the order should still be the same, and actually after correcting the query the order is still different. The only thing I do different in both queries is the extra field! Any idea? Thanks
resig
You cannot guarantee the order of the data coming back from a query therefore you have to explicitly set the order by clause.
Rippo
But I do the order by. It just orders it differently for both queries. I don't get why...
resig
@resig: What Rippo says, in your query only the second part of the union has an order by. So the order of the first part is random.
Andomar
But as I said in my previous comment I changed that already and I still get different results..
resig
But you need to explicitly set the order by on more than one column otherwise you will get what appears to be random results when more than 2 rows contain the same data.
Rippo
@resig: This answer was wrong btw. On rereading, you're asking why two rows with the same fieldDate can appear in different order? That's because their ordering is random; it depends on the query plan, the time of day or even the phase of the moon ;)
Andomar
@Andomar, I know your answer was not quite right! I gave an explanation why, never mind not to worry!!!
Rippo
A: 

Can you do this

  select * from ( select 
     field1,field2,fieldDate, 0 as ordercol from table1 
    union all select 
     field1,field2,fieldDate, 1 as ordercol from table2) t1
    order by fieldDate desc, ordercol asc
Rippo
+2  A: 

The ordering based on any fields that you don't explicitly order by is undefined, and the optimizer can change the ordering if it thinks that results in a better execution plan. Given two rows with the exact same value in the order by field you can not depend on them being in any particularly order in relation to each other unless you explicitly order by another field with different values.

Donnie
I was wondering about that as well, good to know...
Dimitri Wetzel
A: 

Straight from the MySQl manual, to user order by on a union you have to parenthesis the individual tables.

(select field1,fieldDate from table1)
union all 
(select field1,fieldDate from table2)
order by fieldDate desc

This is not SQL standards compliant! The code you entered should order the union of both tables but to my surprise MySQL has the above syntax.

The order in which rows with the same fieldDate are returned can differ for each query execution. Usually this order will be the same but you should not count on it. If you want any extra ordering state more order by fields.

Matijs