您的位置:首页 > 数据库 > MySQL

MySql数据库增删改查示例

2016-02-01 15:18 537 查看
——————–建库语句———————————————————–

create database 表名
default character set utf8
collate utf8_general_ci;


——————–建表语句———————————————————–

示例1:

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;


示例2:

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;


———————-改变表的字段属性————————————————

例:修改数据库test下com表的字段name属性重新设置为10个字符长度

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');


———————–查—————————————————————

例:显示表com中所有内容

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()

avg()

count()

复杂查询示例:

列出价格最高的商品价格

select max(shop_price) from goods


不合法的查询XX:

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元以上的商品选出来

分3步:

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;


2.看每个人的挂科情况

select name,score<60 from stu;


3.计算每个人的挂科科目

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: