首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MYSQL和空值排序

MYSQL和空值排序
EN

Stack Overflow用户
提问于 2014-02-01 10:34:26
回答 1查看 51关注 0票数 0

我有一个名为users的表,其中一些列具有空值,即user_countryuser_state_user_cityuser_areauser_pincode

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

例如:

代码语言:javascript
运行
复制
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

它应该按如下方式排序:期望输出

代码语言:javascript
运行
复制
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

我的代码:

代码语言:javascript
运行
复制
$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');
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-01 10:37:14

您可以使用以下查询:

代码语言:javascript
运行
复制
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)

请看小提琴这里

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21496938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档