mysql limit where_mysql select ,where,limit查询

1、使用查询where、limit

where:查询条件

limit:限制查询结果显示的数量

题目:查询students表中age<25的数据,并显示这些数据的id 、name、age

mysql>select id,name,age from students where  age <25  limit  2;

+----+------+-----+

| id | name | age |

+----+------+-----+

|  1 | lili |  12 |

|  3 | jujo |  21 |

+----+------+-----+

2 rows in set (0.00 sec)

2、使用where

题目:查找students表中name="lili"的所有数据

mysql> select * from students where name ="lili";

+----+------+-----+-----+-------------+

| id | name | sex | age | tel        |

+----+------+-----+-----+-------------+

|  1 | lili | f  |  12 | 13501665963 |

|  7 | lili | f  |  11 | 15110021003 |

+----+------+-----+-----+-------------+

2 rows in set (0.00 sec)

3、使用BINARY 区分大小写(只会查询出大写或者小写的数据):

题目:查询出students表name=“lili”的所有数据

mysql> select * from students where  BINARY  name ="lili";

+----+------+-----+-----+-------------+

| id | name | sex | age | tel        |

+----+------+-----+-----+-------------+

|  1 | lili | f  |  12 | 13501665963 |

|  7 | lili | f  |  11 | 15110021003 |

+----+------+-----+-----+-------------+

2 rows in set (0.00 sec)

没有BINARY 查询出所有name为lili的数据(包括大小写在内)

题目:查询students表name="lili"的所有数据

mysql> select * from studentswherename ="lili";

+----+------+-----+-----+-------------+

| id | name | sex | age | tel        |

+----+------+-----+-----+-------------+

|  1 | lili | f  |  12 | 13501665963 |

|  7 | lili | f  |  11 | 15110021003 |

|  8 | LiLi | f  |  10 | 18710051006 |

+----+------+-----+-----+-------------+

3 rows in set (0.00 sec)

mysql> select*from students whereBINARYname ="LiLi";

+----+------+-----+-----+-------------+

| id | name | sex | age | tel        |

+----+------+-----+-----+-------------+

|  8 | LiLi | f  |  10 | 18710051006 |

+----+------+-----+-----+-------------+

1 row in set (0.00 sec)

4、limit和offset

①limit后面跟的是2条数据,offset后面是从第1条开始读取

mysql> select*from master where id >10  limit  2 offset 1;

②limit后面是从第2条开始读,读取1条信息

mysql> select*from master where id >10limit 2,1;

b1cf78c56251

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL中的LIMIT用于限制结果集中返回的行数。它可以与SELECT语句一起使用,也可以与UPDATE或DELETE语句一起使用。LIMIT的语法是SELECT expression FROM table_name LIMIT offset, count; \[1\] 当使用LIMIT与ORDER BY混用时,MySQL会找到排序后的指定行数后立即返回结果,而不是对整个查询结果进行排序再返回。如果使用索引进行排序,速度会非常快;如果使用文件排序,所有匹配查询的行都会被选中,并且大多数或全部被排序,直到满足LIMIT要求的行数为止。一旦找到了LIMIT要求的行数,MySQL就不会对结果集中剩余的行进行排序了 \[2\]。 然而,实际执行结果可能与我们的想象有所不同。MySQL会对LIMIT进行优化,具体的优化方式可以参考官方文档\[3\]。 所以,当你在MySQL查询中使用WHERE和LIMIT时,它会根据WHERE条件筛选出符合条件的行,并且限制返回的行数。 #### 引用[.reference_title] - *1* [MySQL LIMIT 查询 - 快速简便的指南](https://blog.csdn.net/allway2/article/details/124754760)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [mysql中where,limit于order by 的使用陷阱](https://blog.csdn.net/weixin_39557576/article/details/114827184)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值