I have a problem understandint ORDER BY in MySQL. I have to sort a table by 3 criteria
1 – first i want to sort by TYPE_OF_WORK so all data must be alphabetical, like
dech_rap_bus
dech_rap_bus
ger_dem_dech_bus
ger_dem_dech_bus
ger_dem_stp_pp
...
RESULT => http://sqlfiddle.com/#!2/b2a858/6
2 – second i want to sort by PROJECT_VERSION so all data must be alphabetical, but respecting the 1 criteria, like
dech_rap_bus V123-V1234
dech_rap_bus V300
ger_dem_dech_bus V123-V1234
ger_dem_dech_bus V300
ger_dem_stp_pp V123-V1234
RESULT => http://sqlfiddle.com/#!2/b2a858/7
So 1 and 2 are working perfectly.
3 – and after this i want to sort by the column not_existing
RESULT => http://sqlfiddle.com/#!2/b2a858/5
and i don’t know what it really do, but i see no results … i just want that the
dech_rap_bus V300
where the NOT_EXISTING column is 1 to be at the end, and when are more of NOT_EXISTING = 1 to sort them all but at the end of the table.
I tought to myself that a UNION of 2 selects would help me
/* Selecting all data where not_existing is not 1 or NULL ---> working good! */
(
SELECT
*
FROM
atm_mti_view
WHERE
project_function='FRS01' AND
on_big_project_id = 12 AND
(not_existing != 1 OR not_existing IS NULL)
ORDER BY
type_of_work ASC,
project_version ASC
)
UNION
/* Selecting all data where not_existing is 1 ---> working good! */
(
SELECT
*
FROM
atm_mti_view
WHERE
project_function='FRS01' AND
on_big_project_id = 12 AND
not_existing = 1
ORDER BY
type_of_work ASC,
project_version ASC
)
but what this piece of code does, is putting the not existing dech_rap_bus at the end, good, but it messes up the version sorting, WHY???
SEE RESULT HERE => http://sqlfiddle.com/#!2/b2a858/8
Why is that? I just want to MERGE two select results, what i am doing wrong?
If you do
It will come first.
your query is not ordering because you have different project value for dech_rap_bus