题目来自于:http://www.cnblogs.com/wangfengming/articles/7978183.html
首先创建表 :
-- 创建数据表 CREATE TABLE IF NOT EXISTS tdb_goods( goods_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -- 商品主键 goods_name VARCHAR(150) NOT NULL, -- 商品名称 goods_cate VARCHAR(40) NOT NULL, -- 商品类型 brand_name VARCHAR(40) NOT NULL, -- 商品品牌 goods_price DECIMAL(15,3) UNSIGNED NOT NULL DEFAULT 0, -- 商品价格 is_show BOOLEAN NOT NULL DEFAULT 1, -- 是否上架 is_saleoff BOOLEAN NOT NULL DEFAULT 0 -- 是否打折 ); -- 写入记录 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('R510VC 15.6英寸笔记本','笔记本','华硕','3399',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Y400N 14.0英寸笔记本电脑','笔记本','联想','4899',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('G150TH 15.6英寸游戏本','游戏本','雷神','8499',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X550CC 15.6英寸笔记本','笔记本','华硕','2799',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X240(20ALA0EYCD) 12.5英寸超极本','超级本','联想','4999',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('U330P 13.3英寸超极本','超级本','联想','4299',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('SVP13226SCB 13.3英寸触控超极本','超级本','索尼','7999',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iPad mini MD531CH/A 7.9英寸平板电脑','平板电脑','苹果','1998',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)','平板电脑','苹果','3388',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)','平板电脑','苹果','2788',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('IdeaCentre C340 20英寸一体电脑 ','台式机','联想','3499',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Vostro 3800-R1206 台式电脑','台式机','戴尔','2899',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iMac ME086CH/A 21.5英寸一体电脑','台式机','苹果','9188',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )','台式机','宏碁','3699',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Z220SFF F4F06PA工作站','服务器/工作站','惠普','4288',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('PowerEdge T110 II服务器','服务器/工作站','戴尔','5388',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Mac Pro MD878CH/A 专业级台式电脑','服务器/工作站','苹果','28888',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' HMZ-T3W 头戴显示设备','笔记本配件','索尼','6999',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('商务双肩背包','笔记本配件','索尼','99',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X3250 M4机架式服务器 2583i14','服务器/工作站','IBM','6888',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('玄龙精英版 笔记本散热器','笔记本配件','九州风神','23455',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' HMZ-T3W 头戴显示设备','笔记本配件','索尼','6999',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('商务双肩背包','笔记本配件','索尼','99',DEFAULT,DEFAULT);select * from tdb_goods;
创建好的表如下:
2. 求所有电脑产品的平均价格,并且保留两位小数,AVG,MAX,MIN,COUNT,SUM为聚合函数
select format(avg(goods_price),2) from tdb_goods; -- avg(goods_price的是很多位小数,可以使用format(avg(goods_price),2))将查询结果格式化输出
运行结果:
3.查询所有价格大于平均价格的商品,并且按价格降序排序
select * from tdb_goods where goods_price>(select avg(goods_price) from tdb_goods ) order by goods_price desc;
运行结果:
4.查询类型为“超记本”的商品价格
select goods_name,goods_price from tdb_goods where goods_cate="超级本";
运行结果:
5.查询价格等于"超级本"价格的商品,并且按价格降序排列
select goods_name,goods_price,goods_cate,brand_name from tdb_goods where goods_price in (select goods_price from tdb_goods where goods_cate="超级本")order by goods_price desc;
运行结果:
6.创建“商品类别”表
create table if not exists tdb_goods_cate( cate_id smallint unsigned primary key auto_increment, cate_name varchar(40))desc tdb_goods_cate;
运行结果:
7.查询tdb_goods表的类别记录,并且按"类别"分组
select goods_cate from tdb_goods group by goods_cate;
运行结果:
8.将分组结果写入到tdb_goods_cates数据表(!!!)
就是把一个表的一列结果插入到另一个表格的一列;使用insert 需要增加列的表格名称(要插入的列) select 需要把这一列给别的表 from 另一个表格的名称
insert tdb_goods_cate(cate_name) select goods_cate from tdb_goods group by goods_cate; -- 把一个表格的一列数据插入到另一个表格的一列!!select * from tdb_goods_cate;
运行结果:
9.通过tdb_goods_cates数据表来更新tdb_goods表中的'类别字段'(!!!)
刚才没看懂题目意思:其实就是把tdb_goods表中的goods_cate 变为tdb_goods_cate表中名字相同的cate_id 类别号!!
思路就是首先通过内连接(关联字段是tdb_goods的goods_cate和tdb_goods_cate表的cate_name)建立临时表,更新tdb_goods表的goods_cate;
select * from tdb_goods inner join tdb_goods_cate on tdb_goods.goods_cate = tdb_goods_cate.cate_name; -- 两张表通过goods_cate 与cate_name建立内连接;update tdb_goods inner join tdb_goods_cate on tdb_goods.goods_cate=tdb_goods_cate.cate_name set goods_cate=cate_id; -- 通过内连接建立的临时表,更新goods_cate字段为同行的cate_id;select * from tdb_goods; -- 查看更新过之后的tdb_goods表;
运行结果:
10.通过CREATE...SELECT来 创建[品牌]表 并且同时写入记录
其实有两种方法:
方法一: 首先创建一个品牌表tdb_goods_brand(brand_id ,brand_name) 然后把tdb_goods的brand_name插入到新创建的品牌表:
就是 insert tdb_goods_brand(brand_name) select brand_name from tdb_goods group by brand_name; -----流程跟之前取tdb_goods的goods_cate列插入到tdb_goods_cate是一样的
create table if not exists tdb_goods_brand_1( -- 创建一张新的表tdb_goods_brand brand_id smallint unsigned auto_increment primary key, brand_name varchar(40)) select brand_name from tdb_goods group by brand_name; -- 查询tdb_goods表中的brand_name字段信息(按照brand_name分组)insert tdb_goods_brand_1(brand_name) select brand_name from tdb_goods group by brand_name;select * from tdb_goods_brand_1;
运行结果:
方法二:create table select 的时候就插入字段值:
select * from tdb_goods_brand_1;select brand_name from tdb_goods group by brand_name; -- 按照brand_name分组查找tdb_goods表中的品牌信息,需要把brand_name信息插入到tdb_goods_brand表中;create table if not exists tdb_goods_brand( brand_id smallint unsigned auto_increment primary key, brand_name varchar(40)) select brand_name from tdb_goods group by brand_name;select * from tdb_goods_brand;
运行结果:
11.通过tdb_goods_brands 品牌表 来更新 tdb_goods商品表
思路跟上面通过tdb_goods_cate表的cate_name与tdb_goods表的goods_cate建立关联,然后把tdb_goods的字段goods_cate 替换成对应的tdb_goods_cate表的cate_id字段;
所以操作如下:
select * from tdb_goods inner join tdb_goods_brand on tdb_goods.brand_name=tdb_goods_brand.brand_name; -- 使用内连接通过两张表的brand_name建立连接update tdb_goods inner join tdb_goods_brand on tdb_goods.brand_name=tdb_goods_brand.brand_name set tdb_goods.brand_name=tdb_goods_brand.brand_id -- 对通过brand_name建立的内连接的临时表 更新tdb_goods的brand_name字段,更新为tdb_goods_brand的brand_id字段值;select * from tdb_goods;
运行结果:
12.查看tdb_goods的数据表结构
desc tdb_goods; -- 查看tdb_goods表的结构;
运行结果:
13.通过ALTER TABLE语句修改商品表结构,goods_cate更新为cate_id, brand_name更新为brand_id
alter table tdb_goods change goods_cate cate_id smallint unsigned; -- 其实tdb_goods表中的goods_cate 与brand_name 都是另外两张表tdb_goods_cate tdb_gods_brand的主键alter table tdb_goods change brand_name brand_id smallint unsigned; -- 更新表字段名称使用change(modify只可以更新表字段的属性,不可以改字段名字)desc tdb_goods;
运行结果:
(更改的这两个字段分别是两外两张表的主键,与他们建立联系的关联字段,其实可以设为外键,便于查找);
14.分别在tdb_goods_cates(类别表)和tdb_goods_brands(品牌表)插入记录
insert into tdb_goods_cate(cate_name) values("路由器"),("交换机"),("网卡");insert into tdb_goods_brand(brand_name) values("海尔"),("清华同方"),("神州");
15.在tdb_goods数据表写入任意记录
INSERT into tdb_goods(goods_name,cate_id,brand_id,goods_price) VALUES('LaserJet Pro P1606dn 黑白激光打印机','12','4','1849');
16.查询所有商品的详细信息(通过内连接实现)
select goods_id,goods_name,cate_id,tdb_goods_join_cate.brand_id,goods_price,is_show,is_saleoff,cate_name,brand_name from(select goods_id,goods_name,tdb_goods.cate_id,tdb_goods.brand_id,goods_price,is_show,is_saleoff,cate_name from tdb_goods inner join tdb_goods_cate on tdb_goods.cate_id=tdb_goods_cate.cate_id)as tdb_goods_join_cateinner join tdb_goods_brand on tdb_goods_join_cate.brand_id = tdb_goods_brand.brand_id;
运行结果:
啊 我写的太麻烦了(先连接两个,然后使用前两个连接的结果跟第三个表再进行内连接,,,,)
其实简便方法如下:
select goods_id,goods_name,g.cate_id,cate_name,g.brand_id,brand_name from tdb_goods as ginner join tdb_goods_cate as c on g.cate_id=c.cate_idinner join tdb_goods_brand as b on g.brand_id=b.brand_id;
运行结果:
17.查询所有商品的详细信息(通过左外连接实现)
select goods_id,goods_name,g.cate_id,g.brand_id,cate_name,brand_name,goods_price,is_show,is_saleoff from tdb_goods as gleft join tdb_goods_cate as c on g.cate_id = c.cate_id left join tdb_goods_brand as b on g.brand_id=b.brand_id;
运行结果:
18.查询所有商品的详细信息(通过右外连接实现)
select goods_id,goods_name,g.cate_id,g.brand_id,goods_price,is_show,is_saleoff,cate_name,brand_name from tdb_goods as gright join tdb_goods_cate as c on g.cate_id=c.cate_idright join tdb_goods_brand as b on g.brand_id=b.brand_id;
19.无限分类的数据表设计
CREATE TABLE tdb_goods_types( type_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, type_name VARCHAR(20) NOT NULL, parent_id SMALLINT UNSIGNED NOT NULL DEFAULT 0); INSERT tdb_goods_types(type_name,parent_id) VALUES('家用电器',DEFAULT);INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑、办公',DEFAULT);INSERT tdb_goods_types(type_name,parent_id) VALUES('大家电',1);INSERT tdb_goods_types(type_name,parent_id) VALUES('生活电器',1);INSERT tdb_goods_types(type_name,parent_id) VALUES('平板电视',3);INSERT tdb_goods_types(type_name,parent_id) VALUES('空调',3);INSERT tdb_goods_types(type_name,parent_id) VALUES('电风扇',4);INSERT tdb_goods_types(type_name,parent_id) VALUES('饮水机',4);INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑整机',2);INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑配件',2);INSERT tdb_goods_types(type_name,parent_id) VALUES('笔记本',9);INSERT tdb_goods_types(type_name,parent_id) VALUES('超级本',9);INSERT tdb_goods_types(type_name,parent_id) VALUES('游戏本',9);INSERT tdb_goods_types(type_name,parent_id) VALUES('CPU',10);INSERT tdb_goods_types(type_name,parent_id) VALUES('主机',10);select * from tdb_goods_types;
运行结果:
20.查找所有分类及其父类(将自身作为临时表使用)
select type_id,type_name,parent_id,(select type_name from tdb_goods_types where type_id=t1.parent_id)as parent_name from tdb_goods_types as t1; -- 括号里面的查询语句是查找parent_id对应的type_name 将查询结果作为一个字段parent_name
运行结果:
21. 复制tdb_goods表中编号为12,20的两条记录,添加到tdb_goods表格中:
select * from tdb_goods where goods_id in (12,20); -- 先查询到tdb_goods表中goods_id 为12 20 的这两条数据insert into tdb_goods(goods_name,cate_id,brand_id,goods_price,is_show,is_saleoff) select goods_name,cate_id,brand_id,goods_price,is_show,is_saleoff from tdb_goods where goods_id in (12,20); -- 往tdb_goods表格中插入两条重复的数据;select * from tdb_goods;
运行结果:
22.查找重复记录(!!!)竟然没想到!!
select * from tdb_goods;select GROUP_CONCAT(goods_id) from tdb_goods group by goods_name having count(goods_name)>=2; -- 可以显示重复的一组都有哪些goods_id(按照goods_name分组)select goods_id,goods_name,goods_price from tdb_goods group by goods_name having count(goods_name)>=2;
运行结果:
23. 删除重复记录
select max(goods_id) from tdb_goods group by goods_name having count(goods_name)>=2; -- 把重复的goods_id(选一组里重复的goods_id大的那个选出来删掉)筛选选出来select goods_id from tdb_goods group by goods_name having count(goods_name)>=2; -- 筛选重复的goods_iddelete from tdb_goods_copy where goods_id in (select max(goods_id) from tdb_goods group by goods_name having count(goods_name)>=2);select * from tdb_goods_copy;
运行结果: