mysql数据库增删改查示例
2015-11-08 22:17
676 查看
--------------------建库语句------------------------------------------------------
--------------------建表语句-----------------------------------------------------------
create table com
----------------------改变表的字段属性---------------------------------------------------
--------------------------插入一行信息-----------------------------------------------------
注:日期一定要加单引号''!!!
---------------------------显示表中所有内容-------------------------------
-----------------------改-----------------------------------------------------------------
-------------------删--------------------------------------------------------------------
--------------------------------where 子句详解-----------------------------------------------
列出市场价格比本店价格高于200的goods_id,goods_name,shop_price,market_price项:
--------------模糊查询-------------------------------------
例: 查找“诺基亚”开头的所有商品
select * from goods where goods_name like '诺基亚%';
like->像
%-->通配任意长度字符
'_'--->通配单个字符
----------------------------------------------------------------
//group by(分组) 详解(常用于统计)
---------------------------Mysql提供的 5 个统计(聚合)函数:-------------------------------------------------------------
max() min() sum() avg() count()
列出价格最高的商品价格
select max(shop_price) from goods
//不合法的查询
select goods_id,goods_name,max(shop_price) from goods;
//以cat_id分组,列出每组中价格最高商品的id,价格
select cat_id,max(shop_price) from goods group by cat_id;
//查询最便宜的商品价格
select min(shop_price) from goods;
//统计商店里一共有多少库存量
select sum(goods_num) from goods;
//统计店商品的平均价格
select avg(shop_price) from goods;
//统计行数
select count(*) from goods;
--------------------------------------------------------
//按栏目查询最贵商品
select cat_id,max(shop_price) from goods group by cat_id;
//分组查询每个栏目下的商品种类
select cat_id,count(*) from good by cat_id;
//查询本店每个商品比市场价格低多少钱?
提示:把列名当成变量名来看
select goods_id,goods_name,market_price -shop_price from goods;
//查询每个栏目下面积压的货款
select cat_id count(shop_price*goods_num) from goods group by cat_id;
可以给查询结果起别名
select cat_id count(shop_price*goods_num) as hk from goods group cat_id;
----------------------having子句详解------------------------------
(对查询结果进行筛选,必须是where在前!)
//查询出本店价格比市场价低多少钱,并且把低200元以上的商品选出来
1.查询出本店价格比市场价低多少钱
select goods_id,goods_name,market_price-shop_price as sheng from goods;
2.并且把低200元以上的商品选出来
select goods_id,goods_name,market_price-shop_price as sheng from goods having sheng>200;
//同样也可以用where子句做
select goods_id,goods_name,market_price-shop_price as sheng from goods where market_price-shop_price>200;
3.增加条件 栏目3下
select goods_id,goods_name,market_price-shop_price as sheng from goods where cat_id =3 having sheng>200;
//查询积压货款超过2w元的栏目,以及该栏目积压的货款
select goods_id,sum(market_price*goods_num)
as hk
from goods
group by cat_id
having hk>20000;
//having练习例:
设有成绩表stu如下:
姓名 科目 分数
张三 数学 90
张三 语文 50
张三 地理 40
李四 语文 55
李四 政治 45
王五 政治 30
试查询"两门以及两门以上不及格"同学的"平均分"(用having)
陷阱:有的人考了2门 有人考了3门
//要求只用一次select
1.先查所有人的平均分,再进行过滤
select name,avg(score) from stu group by name;
//看每个人的挂科情况
select name,score<60 from stu;
//计算每个人的挂科科目
select name,sum(score<60) from stu group by name;
最终答案:
select name,sum(score<60) as gk ,avg(score) as pj from stu group by name having gk >=2;
create database 表名 default character set utf8 collate utf8_general_ci;
--------------------建表语句-----------------------------------------------------------
create table com
(id int primary key auto_increment, name char(3), age int(3) unsigned, email varchar(30), tel char(11), salary decimal(7,2), riqi date default '2000-01-01' )charset utf8;
create table stu (`id` int not null primary key auto_increment, `name` varchar(10) not null default '', `subject` varchar(10) not null default '', `score` int not null default 0 )default charset=utf8;
----------------------改变表的字段属性---------------------------------------------------
ALTER TABLE `test`.`com` MODIFY COLUMN `name` char(10);
--------------------------插入一行信息-----------------------------------------------------
注:日期一定要加单引号''!!!
insert into com (name,age,email,tel,riqi) values ('刘备',99,'liubei@shu.com','13892582106','2012-05-06');
---------------------------显示表中所有内容-------------------------------
select * from com;
-----------------------改-----------------------------------------------------------------
update com set email='mahcao@xiliang.com', salary=999 where name='马超';
-------------------删--------------------------------------------------------------------
delete from com where name='马超';
--------------------------------where 子句详解-----------------------------------------------
列出市场价格比本店价格高于200的goods_id,goods_name,shop_price,market_price项:
select goods_id,goods_name,shop_price,market_price from goods where market_price-shop_price>200;
--------------模糊查询-------------------------------------
例: 查找“诺基亚”开头的所有商品
select * from goods where goods_name like '诺基亚%';
like->像
%-->通配任意长度字符
'_'--->通配单个字符
----------------------------------------------------------------
//group by(分组) 详解(常用于统计)
---------------------------Mysql提供的 5 个统计(聚合)函数:-------------------------------------------------------------
max() min() sum() avg() count()
列出价格最高的商品价格
select max(shop_price) from goods
//不合法的查询
select goods_id,goods_name,max(shop_price) from goods;
//以cat_id分组,列出每组中价格最高商品的id,价格
select cat_id,max(shop_price) from goods group by cat_id;
//查询最便宜的商品价格
select min(shop_price) from goods;
//统计商店里一共有多少库存量
select sum(goods_num) from goods;
//统计店商品的平均价格
select avg(shop_price) from goods;
//统计行数
select count(*) from goods;
--------------------------------------------------------
//按栏目查询最贵商品
select cat_id,max(shop_price) from goods group by cat_id;
//分组查询每个栏目下的商品种类
select cat_id,count(*) from good by cat_id;
//查询本店每个商品比市场价格低多少钱?
提示:把列名当成变量名来看
select goods_id,goods_name,market_price -shop_price from goods;
//查询每个栏目下面积压的货款
select cat_id count(shop_price*goods_num) from goods group by cat_id;
可以给查询结果起别名
select cat_id count(shop_price*goods_num) as hk from goods group cat_id;
----------------------having子句详解------------------------------
(对查询结果进行筛选,必须是where在前!)
//查询出本店价格比市场价低多少钱,并且把低200元以上的商品选出来
1.查询出本店价格比市场价低多少钱
select goods_id,goods_name,market_price-shop_price as sheng from goods;
2.并且把低200元以上的商品选出来
select goods_id,goods_name,market_price-shop_price as sheng from goods having sheng>200;
//同样也可以用where子句做
select goods_id,goods_name,market_price-shop_price as sheng from goods where market_price-shop_price>200;
3.增加条件 栏目3下
select goods_id,goods_name,market_price-shop_price as sheng from goods where cat_id =3 having sheng>200;
//查询积压货款超过2w元的栏目,以及该栏目积压的货款
select goods_id,sum(market_price*goods_num)
as hk
from goods
group by cat_id
having hk>20000;
//having练习例:
设有成绩表stu如下:
姓名 科目 分数
张三 数学 90
张三 语文 50
张三 地理 40
李四 语文 55
李四 政治 45
王五 政治 30
试查询"两门以及两门以上不及格"同学的"平均分"(用having)
陷阱:有的人考了2门 有人考了3门
//要求只用一次select
1.先查所有人的平均分,再进行过滤
select name,avg(score) from stu group by name;
//看每个人的挂科情况
select name,score<60 from stu;
//计算每个人的挂科科目
select name,sum(score<60) from stu group by name;
最终答案:
select name,sum(score<60) as gk ,avg(score) as pj from stu group by name having gk >=2;
相关文章推荐
- mysql复制报错案例处理(转)
- maven、hibernate连接mysql实例框架
- Mysql next-locking 间隙锁(转)
- Excel数据插入Mysql数据库可能遇到的问题
- mysql 必知必会 笔记
- MySQL学习笔记(7)之字符集和校对规则
- MySQL存储引擎
- MySQL学习笔记(16)之存储过程
- MySQL学习笔记(15)之索引
- MySQL学习笔记(14)之事务
- MySQL学习笔记(13)之储存引擎
- MySQL学习笔记(12)之触发器(trigger)
- MySQL学习笔记(11)之视图(view)
- MySQL学习笔记(10)之select查询语句
- MySQL学习笔记(9)之外键约束
- MySQL学习笔记(8)之实体间的联系
- MySQL学习笔记(6)之设计范式
- MySQL学习(索引、引擎、优化)
- 在Win7 64位上安装mySQL5.1和Navicat10.0
- mysql的主从,主主,半同步