Mysql数据库学习(4)——过滤数据

过滤数据

通常情况下,采用列检索出来的数据范围很大,并非都是我们所需要使用的数据。为了尽快的寻找出所需数据,我们可以使用wher语句进一步缩小数据范围,从而得到我们真正想要的数据。关于where语句的具体使用方法如下:
1. 单纯的where语句:
select cust_id,cust_name from customers
where cust_id=1001;

该语句表示从表customers的cust_id,cust_name列中挑选出cust_id=1001的行。
2.where+操作符:
where语句中的操作符主要有AND、OR、IN、NOT IN、=、!=、<、>、<=、>=、<>(不等于)、NULL、BETWEEN等几种。
select cust_id,cust_name from customers
where cust_id=1001 and cust_id=1002;

该语句表示从表customers的cust_id,cust_name列中挑选出cust_id=1001和cust_id=1002的行。

select cust_id,cust_name from customers
where cust_id=1001 or cust_id=1002;

该语句表示从表customers的cust_id,cust_name列中挑选出cust_id=1001或cust_id=1002的行。

select cust_id,cust_name from customers
where cust_id=1001 between c1005;

该语句表示从表customers的cust_id,cust_name列中挑选出cust_id的值在1001和1005之间的行。

select cust_id,cust_name from customers
where cust_id in(1001 ,1005);

该语句表示从表customers的cust_id,cust_name列中挑选出cust_id的值为1001和1005的行。

select cust_id,cust_name from customers
where cust_id not in(1001 ,1005);

该语句表示从表customers的cust_id,cust_name列中挑选出所有cust_id的值不为1001和1005的行。

select cust_id,cust_name from customers
where cust_country is NULL;

该语句表示从表customers的cust_id,cust_name列中挑选出所有cust_country的值为空的行。

注:AND操作符的优先级要高于OR的优先级;

3.where +like+通配符:
mysql语句中的通配符主要有两种,分别为百分号(%)通配符和下划线()通配符。%表示任何字符出现任意次数;同%一样,表示单个字符,至少出现一次。
select cust_id from customers
where cust_name like 's%e' ;

该方法表示从所有用户中找出所有用户名字头部为s,尾部为e的所有产品。

select cust_id from customers
where cust_name like _tomme' ;

该方法表示从所有用户中找出用户名为以tomme结尾的所有产品。

4.对过滤后的数据进行排序
在where语句之前加入“order by 列名”,可以对过滤后的数据进行排序。
select cust_id ,cust_name from customers
order by cust_id
where cust_name like _tomme' ;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值