一、逻辑运算符
1.分类 AND OR 和NOT三种运算符。
2.格式
WHERE [NOT] 表达式1 逻辑运算符 表达式2
3.实践
例如:查询Users表中2000年后出生的用户,且性别为男的用户姓名,电话号码,出生年月日。
select uid,uname,uphone
from users
where ubirth>='2000-1-1' and usex='男';
例如:查询Goods表中gdPriced不大于50的商品名称。
select gdname,gdprice
from goods
where not (gdprice>=50);
例如:查询Goods表中gdCity值为长沙或者西安,且gdPriced小于等于50的商品名称。
select gdname,gdprice,gdcity
from goods
where(gdcity='长沙' or gdcity='西安') and gdprice<=50;
二、BETWEEN AND 运算符
1、范围查询
2.格式
WHERE 表达式1 [NOT] BETWEEN 初始值 AND 终止值
3.实践
例如查询Goods表中gdpriced在100到500元的商品名称。
select gdname,gdprice
from goods
where gdprice between 100 and 500;
三、IN运算符
1.功能
限制数据查询的范围
2.格式
WHERE 表达式 [NOT] IN (值1,值2,..... 值N)
3.实践
例如:查询Goods表中gdCity为长沙、西安、上海三个城市的商品名称
select gdname,gdcity
from goods
where gdcity in ('长沙','西安','上海');
四、LIKE运算符
1.功能
模糊查询
2.WHERE 列名 [NOT] LIKE '字符串' [ESCAPE '转义字符']
3.实践
例如:查询Users表中gdName为“李”开头的用户姓名、性别和手机号
select uname,usex,uphone
from users
where uname like '李%';
select uname,usex,uphone
from users
where uname like '_湘%';
use onlinedb
select gdcode,gdname,gdprice
from goods
where gdname like '华为p9\_%';