Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL sorting by 3 columns or UNION them

Tags:

sql

mysql

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?

like image 715
Empeus Avatar asked Aug 07 '12 12:08

Empeus


People also ask

How do I sort multiple columns in MySQL?

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.

Does order of columns matter in MySQL?

Yes, column order does matter.

Why does ORDER BY not work with UNION?

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.

Can you UNION tables with different number of columns?

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.


3 Answers

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

like image 200
Charleh Avatar answered Oct 14 '22 02:10

Charleh


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
like image 23
Joe G Joseph Avatar answered Oct 14 '22 00:10

Joe G Joseph


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;
like image 1
Omesh Avatar answered Oct 14 '22 02:10

Omesh