三、过滤数据

数据表中的数据一般都是非常大,我们一般只需要查询特定的数据

3.1 where单一子句
>>> select * from trade WHERE trade_date='20230313'; #匹配查询
>>> select user_id,product_id,order_id form trade WHERE user_id='tom'; #匹配查询特定列
>>> select * from trade WHERE qty <> 50000; #不匹配查询
>>> select * from trade WHERE price between 10.00 and 105.00; #范围查询,还有范围:in
>>> select * from trade WHERE ext_fields IS NULL; #空值检查
where子句操作符:=,<>,!=,>,>=,<,<=, between...and...

3.2 where多子句组合
多个子句组合需要使用逻辑操作符and, or连接(and的优先级高于or);当多个子句组合时,先执行and,再执行or.
>>> select * from trade where user_id='tom' AND trade_date>=20230121;
>>> select * from trade where trade_date=20230101 OR trade_date=20230202;
>>> select * from trade where user_id not in ('tom','bill');
>>> select * from trade where trade_date=20230101 OR trade_date=20230202 AND user_id='tom';
返回结果:满足trade_date=20230101的所有数据 + 满足trade_date=20230202 AND user_id='tom'的所有数据

3.3 通配符过滤
like操作符,模糊搜索。 与%连用。 %表示任意字符出现任意次数; _表示任意字符出现一次。
>>> select * from trade where product_id like 'jet%'; #查找以jet开头的所有产品
>>> select * from trade where product_id like '%com'; #查找以jet结尾的所有产品
>>> select * from trade where product_id like 'j%com'; #查找以j开头,以jet结尾的所有产品
>>> select * from trade where product_id like 'jet_'; #查找以jet开头的所有产品(产品名称长度4个字符)

3.4 过滤后排序
先过滤,再排序
>>> select * from trade where product_id like 'jet%' order by trade_date DESC;



posted @ 2023-04-12 17:51  5250  阅读(40)  评论(0)    收藏  举报