讓我們創(chuàng)建一個(gè)表來(lái)按升序?qū)θ掌诤蜁r(shí)間進(jìn)行排序。創(chuàng)建表的查詢(xún)?nèi)缦?–
mysql> create table SortByDateAndTime -> ( -> UserId int, -> UserName varchar(100), -> IssueDate date, -> IssueTime time -> ); Query OK, 0 rows affected (0.60 sec)
登錄后復(fù)制
使用insert命令在表中插入記錄。查詢(xún)?nèi)缦?–
mysql> insert into SortByDateAndTime values(1,'John','2018-12-16','10:30'); Query OK, 1 row affected (0.14 sec) mysql> insert into SortByDateAndTime values(2,'Bob','2018-12-16','10:10'); Query OK, 1 row affected (0.14 sec) mysql> insert into SortByDateAndTime values(3,'Carol','2018-12-16','10:20'); Query OK, 1 row affected (0.10 sec) mysql> insert into SortByDateAndTime values(4,'Sam','2018-12-16','10:00'); Query OK, 1 row affected (0.15 sec)
登錄后復(fù)制
使用 select 語(yǔ)句顯示表中所有記錄的查詢(xún)?nèi)缦?–
mysql> select *from SortByDateAndTime;
登錄后復(fù)制
輸出
+--------+----------+------------+-----------+ | UserId | UserName | IssueDate | IssueTime | +--------+----------+------------+-----------+ | 1 | John | 2018-12-16 | 10:30:00 | | 2 | Bob | 2018-12-16 | 10:10:00 | | 3 | Carol | 2018-12-16 | 10:20:00 | | 4 | Sam | 2018-12-16 | 10:00:00 | +--------+----------+------------+-----------+ 4 rows in set (0.00 sec)
登錄后復(fù)制
這是按降序?qū)θ掌诤蜁r(shí)間進(jìn)行排序的查詢(xún) –
mysql> select UserId,UserName,date(IssueDate) as date1,IssueTime from SortByDateAndTime -> order by date(IssueDate)desc,IssueTime desc;
登錄后復(fù)制
以下是以排序的日期和時(shí)間顯示的輸出 –
+--------+----------+------------+-----------+ | UserId | UserName | date1 | IssueTime | +--------+----------+------------+-----------+ | 1 | John | 2018-12-16 | 10:30:00 | | 3 | Carol | 2018-12-16 | 10:20:00 | | 2 | Bob | 2018-12-16 | 10:10:00 | | 4 | Sam | 2018-12-16 | 10:00:00 | +--------+----------+------------+-----------+ 4 rows in set (0.00 sec)
登錄后復(fù)制
或者您可以使用另一個(gè)查詢(xún)來(lái)對(duì)日期和時(shí)間進(jìn)行排序。查詢(xún)?nèi)缦?–
mysql> select UserId,UserName,date(IssueDate) as date1,IssueTime from SortByDateAndTime -> order by date(IssueDate) desc,IssueTime asc;
登錄后復(fù)制
輸出
+--------+----------+------------+-----------+ | UserId | UserName | date1 | IssueTime | +--------+----------+------------+-----------+ | 4 | Sam | 2018-12-16 | 10:00:00 | | 2 | Bob | 2018-12-16 | 10:10:00 | | 3 | Carol | 2018-12-16 | 10:20:00 | | 1 | John | 2018-12-16 | 10:30:00 | +--------+----------+------------+-----------+ 4 rows in set (0.00 sec)
登錄后復(fù)制
以上就是MySQL中按日期和時(shí)間降序排序?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!