L
L
LittleFatNinja2015-12-21 10:52:24
MySQL
LittleFatNinja, 2015-12-21 10:52:24

How to make a selection sorted by two fields in mysql?

there is a table

col1 | col2
     |
null | 2
1    | null
null | 4
3    | null
null | 6
5    | null

you need to somehow select by two fields, so that it would turn out: 1,2,3,4,5,6
if you execute SELECT * FROM table ORDER BY col1, col2, then the selection is 2,4,6,1,3,5, that is, sorted by each of these fields separately

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2015-12-21
@LittleFatNinja

SELECT COALESCE(col1,col2) as someName
ORDER BY someName
UPD: Just take your time and figure out how the COALESCE function works. If you have both fields null or both have a value, you may need different results and a different approach.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question