这样的需求sql怎么写?
一个排序的sql
按status 字段倒序
如果status=1 time字段正序,如果status=0 time字段倒序
下面是需要出来的结果
id status time
5 1 50
6 1 51
1 0 99
10 0 1
回复讨论(解决方案)
select * from (select * from 表 where status = 1 order by time)aunion allselect * from (select * from 表 where status = o order by time desc)b
select * from table order by status desc,if(status=1,'time asc','time desc');
order by time (case when status = 1 then desc else asc end)
select * from (select * from 表 where status = 1 order by time)aunion allselect * from (select * from 表 where status = o order by time desc)b
不行的,我试过, order要起作用,后面必须要跟limit
select * from table order by status desc,if(status=1,'time asc','time desc');
这种写法也试过,无效的 if不能这么用
去试了没?
你用的什么数据库?
select * from table order by status desc,if(status=1,'time asc','time desc');
这种写法也试过,无效的 if不能这么用
select * from tbl_name order by status=1 desc, time*if(status=1,1,-1)
其中
time*if(status=1,1,-1) 将 status=0 的 time 变成负数,以适应整体的升序排列
status=1 desc 将所有 status=1 的排在最前面
order by time (case when status = 1 then desc else asc end)
这样写直接报错了
改成 order by (case when status = 1 then 'time desc' else 'time asc' end)
测试不行,跟union 效果一样
select * from tbl_name order by status=1 desc, time*if(status=1,1,-1)
其中
time*if(status=1,1,-1) 将 status=0 的 time 变成负数,以适应整体的升序排列
status=1 desc 将所有 status=1 的排在最前面 这只适用于它这个表
order by time (case when status = 1 then desc else asc end)
这样写直接报错了
改成 order by (case when status = 1 then 'time desc' else 'time asc' end)
测试不行,跟union 效果一样 select * from table order by status=1 desc, status=0 asc 简单粗暴实用
select * from tbl_name order by status=1 desc, time*if(status=1,1,-1)
其中
time*if(status=1,1,-1) 将 status=0 的 time 变成负数,以适应整体的升序排列
status=1 desc 将所有 status=1 的排在最前面 这只适用于它这个表
这个有用,但IF查询所有行进行运算再对比效率有点低了,我曾经想写个函数(实现功能通if),后来还是决定多建个字段,计算后按这个字段desc
但这样缺少灵活性,如果有其他特殊排序可能还要建字段,然后就又把排序字段纵向切割出来了....(针对大数据量)
order by time (case when status = 1 then desc else asc end)
这样写直接报错了
改成 order by (case when status = 1 then 'time desc' else 'time asc' end)
测试不行,跟union 效果一样 select * from table order by status=1 desc, status=0 asc 简单粗暴实用
这句实现不了的...
select * from (select * from 表 where status = 1 order by time)aunion allselect * from (select * from 表 where status = o order by time desc)b
不行的,我试过, order要起作用,后面必须要跟limit
你是什么数据库?
SELECT * FROM (SELECT * FROM `table2` WHERE `status`=1 ORDER BY time ASC) aUNION ALLSELECT * FROM (SELECT * FROM `table2` WHERE `status`=0 ORDER BY time DESC) b
我在mysql 5.6上运行是OK的
select * from (select * from 表 where status = 1 order by time)aunion allselect * from (select * from 表 where status = o order by time desc)b
不行的,我试过, order要起作用,后面必须要跟limit
你是什么数据库?
SELECT * FROM (SELECT * FROM `table2` WHERE `status`=1 ORDER BY time ASC) aUNION ALLSELECT * FROM (SELECT * FROM `table2` WHERE `status`=0 ORDER BY time DESC) b
我在mysql 5.6上运行是OK的
mysql 5.5.24

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

PHP日志记录对于监视和调试Web应用程序以及捕获关键事件,错误和运行时行为至关重要。它为系统性能提供了宝贵的见解,有助于识别问题并支持更快的故障排除

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸
