gpt4 book ai didi

MYSQL Null 和空值排序

转载 作者:行者123 更新时间:2023-11-29 08:11:27 26 4
gpt4 key购买 nike

我有一个名为 users 的表,其中一些列具有空值,即 user_countryuser_state_user_cityuser_area用户密码

我想按计数对空值进行排序。

例如:

user_id user_country user_state user_city user_area user_pincode
1 1 1 1 1 1 - all values
2 1 1 1 2 - 1 empty or null
3 1 1 - 3 empty or null
4 1 1 1 - 2 empty or null
5 1 - 4 empty or null

它应该像下面这样排序:期望输出

user_id user_country user_state user_city user_area user_pincode
2 1 1 1 1 - 1 empty or null
4 1 1 1 - 2 empty or null
3 1 1 - 3 empty or null
5 1 - 4 empty or null
1 1 1 1 1 1 - all values

我的代码:

$where1 = "user_country IS NOT NULL  AND user_state IS NOT NULL AND user_city IS NOT NULL AND user_area_name IS NOT NULL AND user_area_pincode IS NOT NULL  AND user_country!='' AND user_state!='' AND user_city!='' AND user_area_name!='' AND user_area_pincode!=''";
$this->db->where($where1);
$order = "user_city IS NOT NULL";
$this->db->order_by($order,'desc');

最佳答案

您可以使用此查询:

SELECT yourtable.*
FROM yourtable
ORDER BY
CONCAT(user_country,user_state,user_city,user_area,user_pincode) IS NOT NULL,
(user_country IS NULL) +
(user_state IS NULL) +
(user_city IS NULL) +
(user_area IS NULL) +
(user_pincode IS NULL)

请参阅 fiddle here .

关于MYSQL Null 和空值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21496938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com