php - 具体如何订购?

标签 php mysql sql

Mysql表:

id | category    | image<br>
--------------------------------
0  | landscape   | image name
1  | panarimic   | image name
2  | portrait    | image name
3  | landscape   | image name
4  | landscape   | image name
5  | portait     | image name

我想编写mysql select,它应该首先获取景观,然后列出其他景观。我怎样才能做到这一点?我可以添加类别编号并进行“排序依据”,但是没有它还有其他方法吗?

最佳答案

是的,您可以在 ORDER BY CLAUSE 中使用 CASE EXPRESSION :

SELECT t.id,t.category,t.image
FROM YourTable t
ORDER BY CASE WHEN t.category = 'landscapes' then 1 else 0 end DESC,
         t.category

关于php - 具体如何订购?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36156418/

相关文章:

sql - 将表情符号/表情符号添加到 SQL Server 表

sql - 溶胶/冷聚变 : INSERTS with multiple foreign keys from session

php - Ubuntu 11.04 中的 MongoDB 和 php

PHP正则表达式问题

javascript - 如何使用 if 与 $http.post 的数据回调?

php - 数据库 : String Json vs new column

php - 如何在 CodeIgniter 中编写给定的查询

java - Tomcat 已启动但未创建数据库表且本地主机为 :8080 shows "HTTP Status 404"

mysql - 获取mysql中日期之间的天数差异

sql - 如何跟踪引用表的历史变化?