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?
Use the ORDER BY clause to sort the result set by one or more columns. Use the ASC option to sort the result set in ascending order and the DESC option to sort the result set in descending order. The ORDER BY clause is evaluated after the FROM and SELECT clauses.
Yes, column order does matter.
From MySQL documentation: ... use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Basically the only time an ORDER in a union will be useful is if you are using LIMIT as well.
The columns of joining tables may be different in JOIN but in UNION the number of columns and order of columns of all queries must be same.
Doesn't this give you what you want?
http://sqlfiddle.com/#!2/b2a858/26
Just sort by not_existing first?
What's the problem here? You have your sort but with not_existing records at the end - these are also sorted in the same way
If you do
order by 
 (case when not_existing is null then 0 else not_existing end) desc
,type_of_work ASC,
project_version ASC
It will come first.
your query is not ordering because you have different project value for dech_rap_bus
TYPE_OF_WORK    PROJECT_VERSION 
dech_rap_bus    V123-V1234  
dech_rap_bus    V300
                        You don't need UNION you can achieve this with this query:
SELECT *
FROM atm_mti_view
WHERE project_function='FRS01' AND
      on_big_project_id = 12          
ORDER BY IF(not_existing = 1, 1, 0) ASC,
         type_of_work ASC,
          project_version ASC;
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With