When I perform a LEFT JOIN on two (or more tables), and ORDER BY xxx DESC a column which may produce NULLs (because of incomplete match between the two tables), the NULLs are ranked at the top, with the highest non-NULL values following, and so on.
Is there a simple way via MySQL to force the NULL results to ranked at the bottom, rather than the top?
Here's an example:
I've got a set of tables, T1 & T2
T1 has columns (id, Val1), where id is auto-incremented, and unique
T2 has columns (id, Val2) where id is unique, but is taken from the corresponding value in T1.
For every entry in T2, there is a corresponding entry in T1; but there is not always an entry in T2 for each in T1.
So:
T1
----
id | Val1
00 | 3
01 | 4
02 | 2
03 | 5
T2
---
i2 | Val2
00 | A
02 | Q
SELECT * FROM T1 LEFT JOIN T2 on T2.id = T1.id ORDER BY T1.id DESC
produces
id | Val1 | id | Val2
01 | __4_ | __ | ___
03 | __5_ | __ | ___
00 | __3_ | __ | A__
02 | __2_ | __ | Q__
but what I want is
id | Val1 | id | Val2
00 | __3_ | __ | A__
02 | __2_ | __ | Q__
01 | __4_ | __ | ___
03 | __5_ | __ | ___
Any way to do this via MySQL (as opposed to post-processing in the calling PHP program)?
Is there a simple way via MySQL to force the NULL results to ranked at the bottom, rather than the top?
Here's an example:
I've got a set of tables, T1 & T2
T1 has columns (id, Val1), where id is auto-incremented, and unique
T2 has columns (id, Val2) where id is unique, but is taken from the corresponding value in T1.
For every entry in T2, there is a corresponding entry in T1; but there is not always an entry in T2 for each in T1.
So:
T1
----
id | Val1
00 | 3
01 | 4
02 | 2
03 | 5
T2
---
i2 | Val2
00 | A
02 | Q
SELECT * FROM T1 LEFT JOIN T2 on T2.id = T1.id ORDER BY T1.id DESC
produces
id | Val1 | id | Val2
01 | __4_ | __ | ___
03 | __5_ | __ | ___
00 | __3_ | __ | A__
02 | __2_ | __ | Q__
but what I want is
id | Val1 | id | Val2
00 | __3_ | __ | A__
02 | __2_ | __ | Q__
01 | __4_ | __ | ___
03 | __5_ | __ | ___
Any way to do this via MySQL (as opposed to post-processing in the calling PHP program)?