C
C
Chvanikoff2011-11-29 21:21:54
SQL
Chvanikoff, 2011-11-29 21:21:54

SQL - sorting?

There is a table, for example, with the following structure:
CREATE TABLE mytable (
`id` int(8),
`name` varchar(64),
`group` varchar(16)
);
and records:
1|name1|group1
2|name2|group2
3|name3|group3
4|name4|group2
5|name5|group2
6|name6|group1
How to select all records and sort them by group field so that records with group2 were at the beginning in the presence of the ORDER BY `name` condition?
That is, as a result for a specific example, you will need to get
2|name2|group2
4|name4|group2
5|name5|group2
1|name1|group1
3|name3|group3
6|name6|group1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
edogs, 2011-11-29
@Chvanikoff

select *, if(`group`='group2',1,0) as abazaba from mytable order by abazaba desc, `name`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question