使用左连接的MySql枚举排序

使用左连接的MySql枚举排序,mysql,Mysql,我有以下型号: 有多个客户端(客户端表),每个客户端都有一个或多个地址(地址表)。 每个地址可能有一个固定电话或移动电话号码(PhoneNumberstable`): 不幸的是,我不能改变数据库的设计,它是给我的项目 我需要执行的任务是列出所有客户机及其地址,以及每个地址对应的电话号码(如果存在) 问题是,我需要根据电话号码类型对结果进行排序: 首先,我需要列出带有固定电话号码的地址 之后,我需要列出带有手机号的地址 最后,我需要列出不带任何数字的地址 我接受了以下查询: SELECT C

我有以下型号:

有多个客户端(
客户端
表),每个客户端都有一个或多个地址(
地址
表)。
每个地址可能有一个固定电话或移动电话号码(
PhoneNumbers
table`):

不幸的是,我不能改变数据库的设计,它是给我的项目

我需要执行的任务是列出所有客户机及其地址,以及每个地址对应的电话号码(如果存在)

问题是,我需要根据电话号码类型对结果进行排序:

  • 首先,我需要列出带有固定电话号码的地址
  • 之后,我需要列出带有手机号的地址
  • 最后,我需要列出不带任何数字的地址
我接受了以下查询:

SELECT Clients.last_name, Clients.first_name, Addresses.city, PhoneNumbers.line_type, 
PhoneNumbers.line_number
FROM Clients 
INNER JOIN Addresses ON Addresses.client_id = Clients.id
LEFT JOIN PhoneNumbers ON PhoneNumbers.address_id = Addresses.id
ORDER BY Clients.last_name, Clients.first_name, FIELD(PhoneNumbers.line_type, 'landline','mobile');
问题是没有任何编号的地址首先列出:

+-----------+------------+-----------+-----------+-------------+
| last_name | first_name | city      | line_type | line_number |
+-----------+------------+-----------+-----------+-------------+
| Gracia    | Maria      | Madrid    | NULL      | NULL        |
| Gracia    | Maria      | Madrid    | landline  | 911111111   |
| Gracia    | Maria      | Madrid    | mobile    | 666111111   |
+-----------+------------+-----------+-----------+-------------+
知道如何使这些条目在每个客户端的末尾列出吗?

字段(NULL,{values list})
始终返回NULL

NULL是按“始终”顺序排列的最小值

使用
orderby…,PhoneNumbers.line\u type为空,PhoneNumbers.line\u type='mobile'

PhoneNumbers.line\u类型为空
将空的行放在最后


PhoneNumbers.line\u type='mobile'
将值为
'mobile'
的行放在另一个值之后(但在上一个排序步骤中最后移动的空值之前)。

我发现使用
case
表达式会更简单:

order by client.last_name, client_first_name,
    case phonenumbers.line_type
        when 'landline' then 1
        when 'mobile'   then 2
        else 3
    end
或者,您可以使用
null
-safe运算符:

order by client.last_name, client_first_name,
    phonenumbers.line_type <=> 'landline' desc,
    phonenumbers.line_type <=> 'mobile' desc
order by client.last\u name、client\u first\u name、,
PhoneNumber.line_类型“固定线路”描述,
phonenumbers.line_类型“mobile”desc

在集合中查找是您的朋友:-)您可以按如下方式使用

ORDER BY FIND_IN_SET(if(line_type IS NULL,"",line_type),"landline,mobile,");
样本

MariaDB [(none)]> set @v:=NULL;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select FIND_IN_SET(if(@v IS NULL,"",@v),"landline,mobile,");
+------------------------------------------------------+
| FIND_IN_SET(if(@v IS NULL,"",@v),"landline,mobile,") |
+------------------------------------------------------+
|                                                    3 |
+------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> set @v:="landline";
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select FIND_IN_SET(if(@v IS NULL,"",@v),"landline,mobile,");
+------------------------------------------------------+
| FIND_IN_SET(if(@v IS NULL,"",@v),"landline,mobile,") |
+------------------------------------------------------+
|                                                    1 |
+------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> set @v:="mobile";
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select FIND_IN_SET(if(@v IS NULL,"",@v),"landline,mobile,");
+------------------------------------------------------+
| FIND_IN_SET(if(@v IS NULL,"",@v),"landline,mobile,") |
+------------------------------------------------------+
|                                                    2 |
+------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>
将@v更改为您的字段,并将FIND_放入您的订单中