MYSQL order by 排序的一个小问题探究
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MYSQL order by 排序的一个小问题探究,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5162字,纯文字阅读大概需要8分钟。
内容图文
![MYSQL order by 排序的一个小问题探究](/upload/InfoBanner/zyjiaocheng/474/2a23775499ae48be8fa3af61ba3d9d2b.jpg)
我的理解应该是都会报 Subquery returns more than 1 row 但是从mysql的运行结果来看不是这样的
继续看查询多条结果的情况
mysql> select * from `sql` where id=1 order by (select 1 union select 2); +----+------+ | id | new | +----+------+ | 1 | test | +----+------+ 1 row in set mysql> select * from `sql` order by (select 1 union select 2); 1242 - Subquery returns more than 1 row mysql> select * from `sql` where id in(1,2) order by (select 1 union select 2); 1242 - Subquery returns more than 1 row mysql> select * from `sql` where id in(1,2); +----+--------------------+ | id | new | +----+--------------------+ | 1 | test | | 2 | sqlinjection test2 | +----+--------------------+ 2 rows in set
一条结果的时候不报错,多条结果报错。
经过DBA大牛的指点,DBA这样说的:
只要不走全表扫描,通过主键或者唯一索引能够定位到数据,就可以忽略后面的错误排序语法
如果走全表扫描或者查询的数据不止一条,就有排序语法错误
然后让执行
mysql> explain select * from `sql` where id=1; +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ | 1 | SIMPLE | sql | const | PRIMARY | PRIMARY | 4 | const | 1 | | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ 1 row in set
mysql> explain select * from `sql` where id=1; +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ | 1 | SIMPLE | sql | const | PRIMARY | PRIMARY | 4 | const | 1 | | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ 1 row in set mysql> explain select * from `sql` where id in (1,2); +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | sql | range | PRIMARY | PRIMARY | 4 | NULL | 2 | Using where | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ 1 row in set mysql> explain select * from `sql`; +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | sql | ALL | NULL | NULL | NULL | NULL | 24 | | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ 1 row in set
mysql> select * from `sql` where id=1 order by (select 1 union select 2); +----+------+ | id | new | +----+------+ | 1 | test | +----+------+ 1 row in set mysql> select * from `sql` where id in (1,2) order by (select 1 union select 2); 1242 - Subquery returns more than 1 row mysql> select * from `sql` order by (select 1 union select 2); 1242 - Subquery returns more than 1 row mysql>
可以看到 type 为 const 的时候不会报错。
百度到 type 各个值的含义
type=const表示通过索引一次就找到了; type=all,表示为全表扫描; type=ref,因为这时认为是多个匹配行,在联合查询中,一般为REF。
然后继续来看表结构
这里id 为主键,那么我们把id主键去掉,在执行上面的语句。
mysql> select * from `sql` where id=1 order by (select 1 union select 2); 1242 - Subquery returns more than 1 row mysql> explain select * from `sql` where id=1; +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | sql | ALL | NULL | NULL | NULL | NULL | 24 | Using where | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 1 row in set
结果就显示了错误。
结论:
type为const,索引一次就能找到结果的查询会自动忽略order by 的排序错误,直接查询到结果。
MYSQL order by 排序的一个小问题探究
标签:st表 primary pre 指点 .com where image weight test
本文系统来源:http://www.cnblogs.com/depycode/p/6265938.html
内容总结
以上是互联网集市为您收集整理的MYSQL order by 排序的一个小问题探究全部内容,希望文章能够帮你解决MYSQL order by 排序的一个小问题探究所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。