数据库学习笔记——数据过滤

  • where语句:
select id from product where ...
  • =:等于
  • <>:不等于
  • !=:不等于
  • <:小于
  • <=:小于等于
  • >:大于
  • >=:大于等于
  • is null:空值
  • is not null:非空值
  • between操作符:在指定两个值之间(检索结果包含两个边界值)
select id from product where id=1;
查找id为1的产品id

select id from product where name="榴莲牛奶";
查找name为榴莲牛奶的产品id

select id from product where id<>3; 
查找id不是3的产品id

select id from product where name<>"coco牛奶"
查找产品名称不是coco牛奶的产品id

select id from product where price>=20;
查找价格大于等于20的产品id

select id from product where name is null;
查找产品名称为空的产品id

select id from product where name is not null;
查找产品名称非空的产品id

select id,name,price from product where price between 5 and 10
查找产品价格5<=,并且>=10 的产品id,name,price
  • and操作符:并列条件,都满足才可以被检索出来
  • or操作符:或者条件,只要一个满足即可被检索出来
  • 计算次序:and>or
select id from product where price>10 and channel=1
查找产品价格大于10 且渠道时1的产品id

select id from product where price>10 or channel=1
查找产品价格大于10或者渠道时1的产品id

select id from product where price>10 or channel=1 and madein="China";
相当于:
select id from product where price>10 or (channel=1 and madein="China");
查询结果满足价格大于10或者满足()内的条件即可
  • in ()操作符:指定条件范围,只要匹配范围中的任意一个条件即可
    *** in (数字1,2,3…)或者 in (‘字符1’,‘字符2’,‘字符3’…)
select id from product where price in (10,20);
检索价格时10或者是20的产品id

select id from product where type in ('网红','零食','休闲');
检索标签中含有“网红"、“零食”、"休闲"的产品id

通配符进行过滤

  • like操作符

1、百分号%通配符:表示代表任意个字符(该通配符可以代表0个字符或任意个字符

如:找出名称中符合“****鼓浪屿海岛酒店”的产品:
		select id,name from product where name like "%鼓浪屿海岛酒店"
		即可找到“厦门鼓浪屿海岛酒店”(因为“鼓浪屿”前有%,可以表示多个字符)

2、下划线_通配符:表示代表一个字符(有几个_就代表几个字符)

如:找到名称中符合“鼓浪屿海岛酒店的产品”
select id ,name  from product where name like"_鼓浪屿海岛酒店"
只有一个_找不到"厦门鼓浪屿海岛酒店”

select id,name from product where name like"__鼓浪屿海岛酒店"
可找到“厦门鼓浪屿海岛酒店”(因为“鼓浪屿”前有两个下划线)

3、备注:mysql本身是不区分字母的大小写的, 可使用binary来区分字母的大小写

如:
	select id,name from product where name like"tom";
	此时可以找到name为Tom的产品(因为默认不区分大小写的)
		
	select id,name from product where binary name like"tom";
	此时不能找到名称为Tom的产品(因为binary区分了大小写)

4、备注:不要过度使用通配符,因为使用通配符条件检索速度很慢
5、备注:通配符的位置,最好放在搜索模式的最后面,因为放在开始的位置,检索速度会很慢

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章