MySQL order by 简单解析

1. 语句

SELECT distinct first_letter
FROM tb_customer
WHERE (deleted = 0) ORDER BY first_letter = '#' asc,first_letter;

2. 个人理解

order by first_letter = '#' 相当于做了一个判断 first_letter == "#" ? 1 : 0;

3. SQL完整写法

SELECT distinct first_letter
FROM tb_customer
WHERE (deleted = 0) ORDER BY if(first_letter = '#',1,0) asc,first_letter asc;

4. 结果

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P


氷落
7 声望1 粉丝

一个年过30,苦苦挣扎的程序员......