“order by”count(columns not null)“

编程入门 行业动态 更新时间:2024-10-26 00:29:44
本文介绍了“order by”count(columns not null)“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一种方法来订购mysql结果的值的值不为null的计数。因此,

[id] [1] [1] [0] [1] [1] = 4

[id] [0] [1] [1] [1] [0] = 3

1] [1] = 2

[id] [1] [0] [0] [0] [0] = 1

在上面的例子中,我忽略了ID列,但在实践中我不在乎。 ID总是不为NULL,所以将它添加到count不会改变结果。

任何人都有任何想法,不涉及对结果进行PHP解析到一个新的数组?我试图将处理部分保留在数据库级别。

解决方案

ORDER BY IF(`a` IS NULL,0,1)+ IF(`b` IS NULL,0,1)... DESC

其中 a , b ,...是字段的名称你需要手动枚举它们)

PS:如果你不知道 0 和 NULL 这:

ORDER BY`a`+`b` 。DESC

对你来说已经够好了

I'm looking at a way to order the mysql results by a count of the columns where the value is not null. Therefor,

[id] [1] [1] [0] [1] [1] = 4

[id] [0] [1] [1] [1] [0] = 3

[id] [0] [0] [0] [1] [1] = 2

[id] [1] [0] [0] [0] [0] = 1

In the above case I'm ignoring the ID column but in practice I wouldn't care. ID is always NOT NULL so adding it to count wouldn't change the results.

Anyone have any idea on this that doesn't involve doing a PHP parse on the result into a new array? I'm trying to keep the processing portion in the DB level.

解决方案

ORDER BY IF(`a` IS NULL, 0, 1) + IF(`b` IS NULL, 0, 1) ... DESC

Where a, b, ... is the names of fields (yes, you need to enumerate them all manually)

PS: if you don't know the difference between 0 and NULL this:

ORDER BY `a` + `b` ... DESC

will be good enough for you

更多推荐

“order by”count(columns not null)“

本文发布于:2023-11-22 05:24:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:count   order   null   columns

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!