mysql中order by 排序用asc和desc不起作用怎么回事
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql中order by 排序用asc和desc不起作用怎么回事相关的知识,希望对你有一定的参考价值。
一张表,里边有'regdate'字段,用select语句查询后排序用desc和asc不起作用什么原因呢?
select * from test where remark = '学生' group by regdate desc
就这条,当时练习的时候怎么都出不来。现在可以了,纳闷。。
需要增加order by 关键字 ??处是你要排序的字段 再后面是 desc降序 asc升序 参考技术B 去专门的论坛或者请教专门的高手看看吧。追加问题不一定能及时解决。本回答被提问者采纳 参考技术C 字段类型是什么。 参考技术D order by 写成了 group by。。。。。
python:mysql之ORDER BY 语句
接下来。装逼开始....
ORDER BY 语句用于对结果集进行排序操作
ORDER BY 语句可以指定任何字段作为排序条件
ORDER BY 语句也可以指定多个字段作为排序条件
ORDER BY 语句也可以指定ASC或DESC关键字设置结果集是升序还是降序
ORDER BY 语句是跟在SQL语句最后面的
ORDER BY 语句默认情况下是升序排序,也就是执行ASC
示例1:终端执行SQL语句,查询到结果集后并默认排序为升序
mysql> select * from test_user_copy;
+----+--------+-------------+----------+-----------+---------------------+
| id | name | mobile | address | country | school |
+----+--------+-------------+----------+-----------+---------------------+
| 1 | JAVA | 18847474541 | Beijing | China | Beijing University |
| 2 | Python | 17748484242 | Nanjing | Singapore | Nanjing University |
| 3 | SQL | 17748484246 | Beijing | China | Beijing University |
| 4 | R | 17748484141 | Shanghai | Singapore | Shanghai University |
+----+--------+-------------+----------+-----------+---------------------+
4 rows in set (0.00 sec)
mysql> select * from test_user_copy order by country;
+----+--------+-------------+----------+-----------+---------------------+
| id | name | mobile | address | country | school |
+----+--------+-------------+----------+-----------+---------------------+
| 1 | JAVA | 18847474541 | Beijing | China | Beijing University |
| 3 | SQL | 17748484246 | Beijing | China | Beijing University |
| 2 | Python | 17748484242 | Nanjing | Singapore | Nanjing University |
| 4 | R | 17748484141 | Shanghai | Singapore | Shanghai University |
+----+--------+-------------+----------+-----------+---------------------+
4 rows in set (0.00 sec)
示例2:python执行SQL语句,查询到结果集后并设置排序为降序
import pymysql
class MysqlData:
def __init__(self, host="localhost", user="root", password="123456", database="testing", port=3306):
# 连接数据库
self.connection = pymysql.connect(host=host, user=user, password=password, database=database, port=port,
charset='utf8', cursorclass=pymysql.cursors.DictCursor)
def modify_data(self, sql, args=None):
"""
Args:
sql: sql语句
args: 接收格式化参数
Returns:
"""
try:
with self.connection:
self.connection.ping(reconnect=True)
with self.connection.cursor() as cursor:
# 执行sql语句
cursor.execute(sql, args=args)
# 提交sql语句
self.connection.commit()
except pymysql.err.MySQLError as _error:
raise _error
def get_data(self, sql, args=None, is_data=False):
"""
Args:
sql: sql语句
args: 接收格式化参数
is_data: 判断获取多条数据还是单个数据,默认获取单个数据
Returns:
"""
try:
with self.connection:
self.connection.ping(reconnect=True)
with self.connection.cursor() as cursor:
# 执行sql语句
cursor.execute(sql, args=args)
# 返回查询出的数据信息
return cursor.fetchall() if is_data else cursor.fetchone()
except pymysql.err.MySQLError as _error:
raise _error
if __name__ == '__main__':
# 初始化类,创建对象
info = MysqlData()
# 调用查询多条数据方法
results = info.get_data("""
SELECT
*
FROM
test_user_copy
ORDER BY
country DESC
""", is_data=True)
# 循环列表,获取数据
for i in results:
print(i)
{'id': 2, 'name': 'Python', 'mobile': '17748484242', 'address': 'Nanjing', 'country': 'Singapore', 'school': 'Nanjing University'}
{'id': 4, 'name': 'R', 'mobile': '17748484141', 'address': 'Shanghai', 'country': 'Singapore', 'school': 'Shanghai University'}
{'id': 5, 'name': 'C++', 'mobile': '17748484141', 'address': 'Shenzhen', 'country': 'France', 'school': 'Shanghai University'}
{'id': 1, 'name': 'JAVA', 'mobile': '18847474541', 'address': 'Beijing', 'country': 'China', 'school': 'Beijing University'}
{'id': 3, 'name': 'SQL', 'mobile': '17748484246', 'address': 'Beijing', 'country': 'China', 'school': 'Beijing University'}
Process finished with exit code 0
从返回的结果集可以看到,country这一列已经做了降序排序...
示例3:python执行SQL语句,查询到结果集后并设置id升序、country为降序
if __name__ == '__main__':
# 初始化类,创建对象
info = MysqlData()
# 调用查询多条数据方法
results = info.get_data("""
SELECT
*
FROM
test_user_copy
ORDER BY
id DESC,
country DESC
""", is_data=True)
# 循环列表,获取数据
for i in results:
print(i)
{'id': 5, 'name': 'C++', 'mobile': '17748484141', 'address': 'Shenzhen', 'country': 'France', 'school': 'Shanghai University'}
{'id': 4, 'name': 'R', 'mobile': '17748484141', 'address': 'Shanghai', 'country': 'Singapore', 'school': 'Shanghai University'}
{'id': 3, 'name': 'SQL', 'mobile': '17748484246', 'address': 'Beijing', 'country': 'China', 'school': 'Beijing University'}
{'id': 2, 'name': 'Python', 'mobile': '17748484242', 'address': 'Nanjing', 'country': 'Singapore', 'school': 'Nanjing University'}
{'id': 1, 'name': 'JAVA', 'mobile': '18847474541', 'address': 'Beijing', 'country': 'China', 'school': 'Beijing University'}
Process finished with exit code 0
从返回的结果集可以看到,优先生效的是id排序...
至此,mysql中使用 ORDER BY 语句查询数据完成...
以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,直接私信留言会及时修正发布;感觉还不错记得点赞呦,谢谢!
未完,待续…
一直都在努力,希望您也是!
以上是关于mysql中order by 排序用asc和desc不起作用怎么回事的主要内容,如果未能解决你的问题,请参考以下文章