首页 / PHP / 这样的需求sql怎么写?
这样的需求sql怎么写?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了这样的需求sql怎么写?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3698字,纯文字阅读大概需要6分钟。
内容图文
一个排序的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
内容总结
以上是互联网集市为您收集整理的这样的需求sql怎么写?全部内容,希望文章能够帮你解决这样的需求sql怎么写?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。