【问题标题】:SQL order by total of number of value by rowsSQL order by total of value by rows
【发布时间】:2021-08-06 17:58:29
【问题描述】:

有没有办法返回行中包含值的列的总和,以将其用于 order by 示例:

id  | title         | body_text_1           | body_text_2           | body_text_3           |
---------------------------------------------------------------------------------------------
1   |  History      | book of History       | movies                | Image                 |
2   |  Sport        | history               | book                  | Audio                 |
3   |  Cinema       | History               | History               | History               |
4   |  History      | classic               | History               | Audio                 |

如果我搜索单词 历史,我正在使用 LIKE %value% 结果显示 id 3 为第一个,因为它在 3 列中有历史记录

第二个将是 1 和 4,因为它在 2 列中有历史记录

最后一个将是 2,因为它只在一列中有历史记录

结果:

3
1
4
2

【问题讨论】:

    标签: php mysql sql


    【解决方案1】:

    您可以在order by 中添加布尔值:

    order by (title = 'History') + (body_text_1 = 'History') +  (body_text_2 = 'History') + (body_text_34 = 'History') desc   
    

    【讨论】:

    • 返回相同的结果是因为我使用的是 %value%
    • @im-learning 。 . .你可以使用任何合适的表达方式。这解决了您最初提出的问题。
    猜你喜欢
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多