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排序方式
有两个思路
1、按照各自的活动状态先排序,插入到临时表,最后再union all所有结果集
create temporary table tmp1select * from tb where 活动状态='筹备中' order by 开始时间
create temporary table tmp2
select * from tb where 活动状态='进行中' and 开始时间 is not null order by 开始时间
create temporary table tmp3
select * from tb where 活动状态='进行中' and 开始时间 is null
create temporary table tmp4
select * from tb where 活动状态='已结束' order by 开始时间 desc
(select * from tmp1)
union all
(select * from tmp2)
union all
(select * from tmp3)
union all
(select * from tmp4)
2、通过field函数自定义排序
select * from tb order by field(活动状态,'筹备中','进行中','已结束') asc,开始时间 asc
但这种只能按指定排序,你这种多种排序,有困难。
可以看看上面两种方法结合或许有更好的方法。
CDatabase类封装了MySQL数据库的功能,因此不具备通用性,只能在对MySQL的应用程序中使用。下面将根据C++要求及规范给出CDatabase类的具体结构以及相关简要介绍:class CDatabase
{ public: BOOL UnLockTable()//解锁 BOOL LockTable(char* TableName,char* PRIORITY)//加锁 int Reload()//重新登陆,非零时返回错误信息 char* GetState()//服务器状态 char* GetServerInfo()//服务器信息 int GetProtocolInfo()//协议信息 char* GetHostInfo()//主机信息 char * GetClientInfo()//客户机信息 char* GetFieldName(int FieldNum)//字段名 BOOL IsEnd()//是否最后 int DropDB(char *db)//删除数据库,非零时返回错误信息 void SeekData(int offset)//查找指定数据 int CreateDB(char *db)//创建数据库,非零时返回错误信息 void FreeRecord()//释放结果集 unsigned int GetFieldNum()//得到字段数 BOOL ConnectDB(Database_Param *p)//连接数据库 MYSQL_ROW GetRecord()//得到结果(一个记录) my_ulonglong GetRowNum()//得到记录数 BOOL SelectDB(Data_Param *para)//选择数据库 BOOL UpdateRecord(Data_Param *para)//更新记录 BOOL SelectRecord(Data_Param *para)//选择记录 BOOL InsertRecord(Data_Param *para)//插入记录 BOOL DelRecord(Data_Param *para)//删除记录 BOOL SelectAll(Data_Param *para)//选择所有记录 char * OutErrors()//输出错误信息 CDatabase()//初始化数据库 virtual ~CDatabase()//关闭数据库连接
private: MYSQL mysql//数据库连接句柄 MYSQL_RES *query//结果集 MYSQL_ROW row//记录集 MYSQL_FIELD *field//字段信息(结构体) BOOL FindSave(char *str)//查找并保存结果集 }
通过CDatabase类中定义的这些功能函数,我们可以通过远程或本机完成对MySQL数据库的绝大部分 *** 控,并且由于定义了解锁和加锁功能,使得应用程序能够多线程或多进程地访问数据库,大大提高了效能。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)