mysql--浅谈查询1
这是对自己学习燕十八老师mysql教程的总结,非常感谢燕十八老师。
依赖软件:mysql5.6
系统环境:win
在谈查询之前,先说一个特别重要的概念
一定将列名看成变量,既然是变量就可以运算
一定将列名看成变量,既然是变量就可以运算
一定将列名看成变量,既然是变量就可以运算
重要的事说三遍
select5种语句之where语句
# 作用:条件查询
# 语法
select 查询项 from 表名 where 表达式;
表达式为真,则取出对应的查询项 # where应用场景
各种条件查询场合,如按学号查询学生,上线时间查产品等
1、比较运算符
1、等于(=)
# 查询商品id为32的商品
select goods_id,cat_id,goods_name,shop_price
from goods
where goods_id=32;
2、不等于(!=/<>)
# 查询不属于第三个栏目的商品
select goods_id,cat_id,goods_name
from goods
where cat_id!=3;
3、大于(>)
# 本店价格高于3000的商品
select goods_id,cat_id,shop_price,goods_name
from goods
where shop_price>3000;
4、小于(<)
# 本店价格低于或等于100的商品
select goods_id,cat_id,shop_price,goods_name
from goods
where shop_price<100;
5、在某个集合内(in)------>离散型
# 查出在栏目4或者栏目11内的商品信息 不用or
select goods_id,cat_id,goods_name
from goods
where cat_id in (4,11);
6、在某个范围内(between...and...)------>连续型
# 取出商品价格在100和500之间的商品,包含100,500 不用and
select goods_id,cat_id,shop_price,goods_name
from goods
where shop_price between 100 and 500;
7、不在某个集合内(not in)------>离散型
#查询不在第3栏和不在第11栏的商品
select goods_id,cat_id,good_name
from goods
where cat_id not in (3,11);
8、列名看成变量并计算
#取出本店价格比市场价省的钱,并且省200以上的
select goods_id,cat_id,market_price-shop_price,goods_name
from goods
where market_price-shop_price >200;
2、逻辑运算符
1、逻辑与(and / &&)
#查询不在第3栏和不在第11栏的商品
select goods_id,cat_id,goods_name
from goods
where cat_id!=3 and cat_id!=11;
2、逻辑或(or / ||)
#查询价格大于100且小于300,或者大于3000且小于4000的商品
select goods_id,cat_id,shop_price,goods_name
from goods
where shop_price between 100 and 300 or shop_price between 3000 and 4000;
3、逻辑非(not / !)
# 查看价格小于500的商品 用>=和not
select goods_id,shop_price,goods_name
from goods
where shop_price not (shop_price>=500);
3、模糊匹配(like)
% 通配任意多个字符
_ 通配任意单一字符
# 查询以诺基亚开头的商品名
select goods_id,cat_id,goods_name
from goods
where goods_name like '诺基亚%';
在这介绍两个字符串函数
cancat() :字符串拼接函数
substring() : 字符串剪切函数
# 取出诺基亚开头的商品并将诺基亚改成HTC
select goods_id,cat_id,concat('HTC',substring(goods_name,4))
from goods
where goods_name like '诺基亚%';
# 将诺基亚开头的商品名称改成HTC开头的
update goods set goods_name=concat('HTC',substring(goods_name,4))
where godds_name like '诺基亚%';
select5种语句之group语句
# 作用:分组
# 语法
select 查询项 from 表名
group by 列名(字段);
注意:查询项必须是分组中有的或者统计函数等函数处理后的结果,否则语意上不对
# 示例
select goods_id,cat_id,goods_name
from goods
group by cat_id;
常配合group语句使用的统计函数(统计函数也可以单独使用)
1、max:求最大值
#示例
# 查询第三个栏目下最贵的价格
select goods_id,cat_id,max(shop_price),goods_name
from goods
group by cat_id;
2、min:求最小值
#示例
# 查询第三个栏目下最便宜的价格
select cat_id,min(shop_price),goods_name
from goods
group by cat_id;
3、sum:求总和
# 一次计算完每个栏目下的库存量之和
select cat_id,sum(goods_number)
from goods
group by cat_id;
4、avg:求平均值
# 按cat_id分组 计算每个栏目下的商品的平均价格
select cat_id,avg(shop_price)
from goods
group by cat_id;
5、求总行数
# select count(*) from 表名:绝对行数,包括null行
# select count(列名) from 表名:不包括null行的行数
# Innodb引擎的表用count(*)效率低 每一行都去数 示例
select count(goods_id)
from goods;
select5种语句之having语句
having和where的异同
相同点:两者都是可以筛选数据
不同点:where针对表中的列发挥作用,查询数据
having针对查询结果中的列发挥作用,筛选数据
# 语法
select 查询项 from 表名 having 表达式 # 示例
# 取出折扣大于200的,类型=3
# as 后面的变量是对前面表达式的命名
select goods_id,cat_id,(market_price-shop_price) as discount
from goods
where cat_id=3
having discount>200;
select5种语句之order语句
desc:降序排列
asc:升序排列
# 作用:排序
# 语法
select 查询项 from 表名 order by 列名 desc/asc # 示例
# 多字段排序
select goods_id,cat_id,shop_price,goods_name
from goods
order by cat_id desc,shop_price asc;
select5种语句之limit语句
# 作用: 限制条数
# 语法
select 查询项 from 表名 limit 偏移量,条目量 # 示例
# 查询出goods表中的前5条数据
select goods_id,cat_id,shop_price,goods_name
from goods
limit 5; # 查询出goods表中价格在前3到前5的商品
select goods_id,cat_id,shop_price,goods_name
from goods
order by shop_price desc
limit 2,3;
select5种语句之间的使用顺序关系
where <——— group by <———— having <———— order by <———— limit
如果出现了多个查询语句顺序必须如上(等级从左到右依次递减)
小练习:
有如下表及数据
要求:查询出2门及2门以上不及格者的平均成绩
# 数字之间的比较运算符结果为1/0 select name,sum(score<60) as gk, avg(score) as avg_score
from stu
group by name
having gk>=2;
由于mysql版本的变化,语法可能存在一定的变化,欢迎指出错误和评论区讨论
最新文章
- 微信为什么发布 Mac 版?
- 格雷码原理与Verilog实现
- ED2k Resource
- 【Win10】让 TextBlock 按字符换行
- 样式表中的 element.style样式如何修改
- iOS开发——使用OC篇&;frame,bounds,center,position,anchorPoint总结
- [CAMCOCO][C#]我的系统架构.服务器端.(四)----Model层 实体的自我验证
- Git错误non-fast-forward后的冲突解决(转载)
- SQL经典笔试题之一
- 为 Web 设计师准备的 25+ 款扁平 UI 工具包
- PowerShell中的数学计算
- shell脚本应用(5)--实用脚本片段
- oracle的resetlogs机制浅析
- (转)Ubuntu中让终端对于历史输出的内容保持足够长
- linux处置服务Iptables
- apue- chapter 1 UNIX基础知识
- spring 里面的StringUtils,先放这儿,有时间研究吧
- 【网摘】C#.NET 在 MVC 中动态绑定下拉菜单的方法
- idea打开dashboard
- Retrofit官方文档翻译