MySql的几种排序方式

MySql的几种排序方式,第1张

1、单列排序

SELECT * FROM test1 ORDER BY date_time

默认升序,降序后面接"DESC"即可。

2、多列排序 

SELECT * FROM test1 ORDER BY `status`, date_time DESC

首先按`status`字段排序,若`status`相等,则按data_time排序。

3、自定义排序

SELECT * FROM test1 ORDER BY FIELD(`status`, 3, 2, 4, 1, 5), date_time DESC

使用"FIELD()"函数,可指定顺序。

4、其他条件排序

先按大于等于当前时间升序,再按小于当前时间降序,支持分页。

SELECT * FROM test1 ORDER BY date_time <NOW(), IF(date_time <NOW(), 0, date_time), date_time DESC

附加SQL脚本:

CREATE TABLE `test1` (

  `id` bigint(20) NOT NULL AUTO_INCREMENT,

  `name` varchar(50) NOT NULL,

  `date_time` datetime NOT NULL,

  `status` int(5) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

INSERT INTO `test1` VALUES

(NULL, '测试1', '2018-03-05 11:09:00', 1),(NULL, '测试2', '2018-03-06 11:09:00', 1),(NULL, 'abc', '2018-03-07 11:09:00', 1), 

(NULL, 'def', '2018-04-08 11:09:00', 2),(NULL, '李某某', '2018-04-17 11:09:00', 1),(NULL, '饭某某', '2018-04-20 13:09:00', 2),

(NULL, '赵', '2018-04-20 01:09:00', 4),(NULL, '倩', '2018-04-28 11:09:00', 2),(NULL, 'andy', '2018-04-30 11:09:00', 1),

(NULL, 'tony', '2018-05-08 11:09:00', 4),(NULL, 'tom', '2018-05-07 11:09:00', 3),(NULL, 'bill', '2018-05-18 11:09:00', 3),

(NULL, 'james', '2018-06-07 11:09:00', 4),(NULL, 'anthony', '2018-06-18 11:09:00', 2),(NULL, '盖茨', '2018-04-21 11:09:00', 1),

(NULL, '部长', '2018-04-24 11:09:00', 4),(NULL, '李总', '2018-04-20 11:09:00', 5),(NULL, '张总', '2018-04-29 11:09:00', 2),

(NULL, '王总', '2018-04-19 11:09:00', 3),(NULL, '唐总', '2018-05-01 11:09:00', 2)

参考的这篇文档Mysql排序方式

打开数据库那就不写了。

前几天刚写了一个。你看下

/* 表结构

CREATE TABLE `lh_categroy` (

`id` int(10) NOT NULL auto_increment,

`parentid` int(6) NOT NULL,

`name` varchar(255) NOT NULL,

`keyword` varchar(255) NOT NULL COMMENT '关键字',

`des` varchar(255) NOT NULL COMMENT '描述',

PRIMARY KEY  (`id`),

UNIQUE KEY `name` (`name`)

) ENGINE=MyISAM AUTO_INCREMENT=44 DEFAULT CHARSET=utf8

*/

/**

* 获得所有栏目排序后的列表

* @return array

*/

function getAllOrderColumns($id = 0) {

global $columns

$result = array()

if ($id) $result[$id] = $columns[$id]

foreach ($columns as $column) {

if ($column['parentid'] == $id) {

$column['level'] = 0

$result[$column['id']] = $column

getColumns($columns, $column['id'], $result, 1)

}

}

return $result

}

function getColumns($columns, $cid, &$result, $l = 1) {

foreach ($columns as $c) {

if ($c['parentid'] == $cid) {

$c['level'] = $l

$result[$c['id']] = $c

getColumns($columns, $c['id'], $result, $l + 1)

}

}

}

$sql = 'select * from lh_categroy'

$query = mysql_query($sql)

while($row = mysql_fetch_assoc($query)){

$columns[]=$row

}

$fenlei = ''

$fenlei =  '<select name="cid" style="width:200pxheight:25px">

<option value="0">请选择分类</option>'

foreach(getAllOrderColumns() as $v){

$v[name] = $v[level] ? ($v[level]==1 ? '&nbsp&nbsp|-'.$v[name]:'&nbsp&nbsp&nbsp|-'.$v[name]) : $v[name]

$fenlei .= "<option value='$v[id]'>$v[name]</option>"

}

$fenlei .= '</select>'

echo $fenlei

f exists (select * from dbo.sysobjects where id = object_id(N'[tb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [tb]

GO

--示例数据

create table [tb]([id] int identity(1,1),[pid] int,name varchar(20))

insert [tb] select 0,'中国'

union all select 0,'美国'

union all select 0,'加拿大'

union all select 1,'北京'

union all select 1,'上海'

union all select 1,'江苏'

union all select 6,'苏州'

union all select 7,'常熟'

union all select 6,'南京'

union all select 6,'无锡'

union all select 2,'纽约'

union all select 2,'旧金山'

go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_id]') and xtype in (N'FN', N'IF', N'TF'))

drop function [dbo].[f_id]

GO

/*--树形数据处理

级别及排序字段

--邹建 2003-12(引用请保留此信息)--*/

/*--调用示例

--调用函数实现分级显示

select replicate('-',b.[level]*4)+a.name

from [tb] a,f_id()b

where a.[id]=b.[id]

order by b.sid

--*/

create function f_id()

returns @re table([id] int,[level] int,sid varchar(8000))

as

begin

declare @l int

set @l=0

insert @re select [id],@l,right(10000+[id],4)

from [tb] where [pid]=0

while @@rowcount>0

begin

set @l=@l+1

insert @re select a.[id],@l,b.sid+right(10000+a.[id],4)

from [tb] a,@re b

where a.[pid]=b.[id] and b.[level]=@l-1

end

return

end

go

--调用函数实现分级显示

select replicate('-',b.[level]*4)+a.name

from [tb] a,f_id()b

where a.[id]=b.[id]

order by b.sid

go

--删除测试

drop table [tb]

drop function f_id

go

/*--结果

中国

----北京

----上海

----江苏

--------苏州

------------常熟

--------南京

--------无锡

美国

----纽约

----旧金山

加拿大

--*/


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8625185.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-19
下一篇2023-04-19

发表评论

登录后才能评论

评论列表(0条)

    保存