mysql数据库增删改查示例

--------------------建库语句------------------------------------------------------
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;



posted @ 2015-11-08 22:17  时光.漫步  阅读(273)  评论(0编辑  收藏  举报