0
点赞
收藏
分享

微信扫一扫

SQL学习笔记——IN, BETWEEN, LIKE运算符

Spinach菠菜 2022-02-06 阅读 187

IN运算符

想得到地址位于弗吉尼亚州,佛罗里达州以及乔治亚州的顾客,可以这样写

select *
from customers
where state='VA' or state='GA' or state='FL'

 但是这样表示非常繁琐,所以我们可以使用IN运算符

select *
from customers
where state in ('VA','GA','FL')

 如果想获取这些州以外的数据,可以使用NOT子句

select *
from customers
where state not in ('VA','GA','FL')

 练习

得到先存货数量为49,38,72的产品

select *
from products
where quantity_in_stock in (49,38,72)

 BETWEEN运算符

想得到积分在1000到3000之间的顾客可以这样写

select *
from customers
where points>=1000 and points<=3000

但是可以使用BETWEEN运算符进行简化

select *
from customers
where points between 1000 and 3000

 BETWEEN运算符包括临界值

 练习

得到1990年1月1日到2000年1月1日之间出生的顾客

select *
from customers
where birth_date between '1990-01-01' and '2000-01-01'

 LIKE运算符

LIKE运算符可以检索遵循特定字符串模式的行

1.得到姓氏以B开头的顾客

select *
from customers
where last_name like 'B%'

2.得到姓氏以Brush开头的顾客

select *
from customers
where last_name like 'Brush%'

 3.得到姓氏中包含字母b的

%b%表示b字母前后均可以有任意多个字符

select *
from customers
where last_name like '%b%'

 4.得到姓氏以Y结尾的顾客

select *
from customers
where last_name like '%y'

 5.得到第六个字母为Y的顾客

%代表任意多个字符,_代表一个字符

select *
from customers
where last_name like '_____y'

练习

得到地址中包含trail或者avenue的顾客

select *
from customers
where address like '%avenue%' or address like '%trail%'

 得到电话号码以9结尾的顾客

select *
from customers
where phone like '%9'

举报

相关推荐

0 条评论