mysql--检索数据(一)

人之所以痛苦,那是因为你在成长。--------magic_guo
数据库:一个以某种有组织的方式存储的数据集合(保存有组织的数据的容器)。
sql检索数据的语法:想选择什么 + 从什么地方选择 + 怎么样选择
使用关键字select:select 数据 from 表 + 选择条件;

检索的单个列
select goods_id from goods;
检索多个列
select goods_id,goods_name,goods_price from goods;
检索所有列
select * from goods;

检索去重:distinct
顾名思义,此关键字指示mysql只返回不同的值,distinct 不能部分使用,应用于所有列而不仅是前置它的列
select distinct goods_id from products;

限制结果:limit
select goods_name from products limit 5;
limit 5, 5;即索引,意思是从行4开始的5条数据。
select goods_name from products limit 5, 5;
selset goods_name from products limit 5 offset 5;

我们要检索的数据如果是无序状态,则对于业务来说是没有意义的,所以我们要建检索数据进行排序
order by 子句:根据字段对结果集进行排序:
select goods_name from goods order by goods_name;

也可以按多个字段进行排序:
select goods_id, goods_name, goods_price from goods order by goods_id, goods_price;
指定排序方向:desc 逆序; asc 正序(默认);
select goods_id, goods_name from goods order by goods_id desc;
desc 只会应用到直接位于它前面的字段。

过滤数据:
使用where子句

select prod_name, prod_price from products where prod_price = 2.50;
where子句操作符:
= 等于
!= 不等于
<> 不等于
< 小于
> 大于
<= 小于等于
>= 大于等于
between 3 between 5 在3和5之间(包括3和5)

组合where子句
and、 or、 in、 not in
select prod_id, prod_price, prod_name from products where vend_id = 1003 and prod_price <= 10;

select prod_name, prod_price from products where vend_id=1002 or vend_id=1003;

and 和 or 组合起来使用,一般先执行and,在执行or;但是推荐使用括号;
select prod_name, prod_price from products where vend_id=1002 or vend_id=1003 and prod_price >= 10;
本来我们想要的是:返回价格大于10元以上且有1002和1003制造的所有产品;
但是先计算了and,vend_id=1003 and prod_price >= 10;然后才计算了vend_id=1002 所以与其结果并不正确
使用括号解决:
select prod_name, prod_price from products where (vend_id=1002 or vend_id=1003) and prod_price >= 10;

in操作符用来指定条件范围,范围中的每个条件都可以进行匹配:
select prod_name, prod_price from products where vend_id in(1002, 1003)order by prod_price;
not in 则是否定其范围:
select prod_name, prod_price from products where vend_id not in(1002, 1003)order by prod_price;

使用通配符进行过滤
like关键字:我们常说的模糊查询,通常与通配符 “%” 和通配符 "",搭配使用。
“%” 表示任意字符出现任意次数;
” 表示只匹配一个字符
select prod_id, prod_name from products where prod_name like “jet%”;
即匹配以jet起头的词,告诉mysql接受jet之后的任意字符,不管他有多少字符。
两头都匹配
select prod_id, prod_name from products where prod_name like “%jet%”;
尾部是jet,从前边匹配
select prod_id, prod_name from products where prod_name like “%jet”;

当然 “_” 也有类似操作:但是只匹配一个字符:
select prod_id, prod_name from products where prod_name like “jet_”;
select prod_id, prod_name from products where prod_name like “_jet”;
select prod_id, prod_name from products where prod_name like “_jet_”;

一般的话不要使用通配符,因为使用通配符,因为使用通配符,则意味着模糊查询,模糊查询的开销相对于还是跟大的,除非迫不得已。

使用正则表达式进行匹配
周所周知正则表达式在匹配上是没有什么能够的,所以在mysql里,也支持正则查询;
关键字:regexp:它告诉mysql,regexp后所跟的东西为正则表达式处理

or匹配:
select prod_name from products where prod_name regexp ‘1000|2000|3000’ order by prod_name;
将匹配prod_name 中有名字为1000或者2000或者3000的数据
也可以使用 [ ],是另外一种形式的or
select prod_name from products where prod_name regexp ‘[123] ton’ order
by prod_name;

匹配范围:
[0-9] = [0123456789] 匹配数字0-9
[a-z] 匹配字母a-z

匹配特殊字符:
使用 “\” 双下划线转义:第一个下划线mysql自己解释,第二个下划线正则表达式库解释。
例如匹配有 . 的产品的名字:
select vend_name from vendors where vend_name regexp ‘\.’ order by vend_name;
\- 则是匹配 - ;
\. 则是匹配 . ;
常用重复元字符:
. :匹配任意1个字符
* :匹配0个或多个
+ :匹配1个或多个
? :匹配0个或1个
{n} :指定数目的匹配
{n,} :不少于指定数目的匹配
{n, m} :匹配数目的范围
更多正则表达式的匹配方法,请见正则官方文档。由于正则表达式匹配非常灵活多变,因此不再一一举例。

欢迎大家批评指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值