首页 > 数据库 > MySQL >

【Mysql】Mysql对记录的操作

2016-09-15

MySQL 操作数据表记录;INSERT插入记录;INSERT [INTO] tb_name [(col_name),…] {VALUES| VALUE};({expr | DEFAULT},…),(…),…;INSERT [INTO] tb_name SET col_name={expr |DEFAULT};INSERT [INTO] tb_name [(col_name)] SELECT …

MySQL 操作数据表记录

INSERT插入记录

INSERT [INTO] tb_name [(col_name),…] {VALUES| VALUE}

({expr | DEFAULT},…),(…),…

INSERT [INTO] tb_name SET col_name={expr |DEFAULT},…

INSERT [INTO] tb_name [(col_name)] SELECT …

此方法可以将查询结果插入到指定数据表。

UPDATE

单表更新

UPDATE [LOW_PRIORITY] [IGNORE]table_reference SET col_name1 = {expr1 |DEFAULT}

[, col_name2 = {expr2 | DEFAULT}] ……

[WHERE where_condition]

DELETE

单表删除

DELETE FROM tb_name [WHERE where_condition]

SELECT

查找记录

SELECT select_expr[,select_expr ….]

[

FROMtable_references

[WHERE where_condition ]

[GROUPBY {col_name | position} [ASC | DESC],….]

[HAVINGwhere_condition]

[ORDERBY {col_name | expr | position} [ASC | DESC],…]

[LIMIT{[offset,] row_count | row_count OFFSET offset}]

]

字段赋予别名

SELECT 字段名 AS 别名

使用where条件过滤

对查询结果分组

[GROUP BY{col_name | position} [ASC |DESC],...] ASC 默认的升序

[HAVING ] 分组条件,分组条件要么用聚合函数(COUNT()、AVG() 只有一个返回值),或者字段必须出现在 select的字段里。

对查询结果进行排序

[ORDER BY{col_name | expr |position} [ASC |DESC],…]

LIMIT

限制返回结果的数量

[LIMIT{[offset,]row_count |row_count OFFSEToffset}]

\

子查询

子查询(Subquery)是指出现在其他SQL语句内的SELECT子句。

子查询指嵌套在查询内部,且必须时钟出现在圆括号内。

子查询可以包含多个关键字或条件,

如DISTINCT /GROUP BY / ORDER BY / LIMIT 函数等。

子查询的外层查询可以是SELECT , INSERT,UPDATE,SET 或 DO。

子查询可以返回标量、一行、一列或子查询。

例如:

SELECT * FROM t1 WHERE col1 = (SELECT col2FROM t2);

其中SELECT * FROM t1,称为 Quter Query / Outer Statement

SELECT col2 FROM t2,称为SubQuery

子查询分类

1. 使用比较运算符的子查询

使用比较运算符的子查询

= / > / < / >= / <= / <> / != / <=>

用ANY、SOME、ALL修饰的比较运算符

Operand comparison_operator ANY (subquery)

Operand comparison_operator SOME(subquery)

Operand comparison_operator ALL(subquery)

\

1. 使用 [NOT] IN 的子查询

Operand comparison_operator [NOT] IN (subquery)

=ANY 运算符与IN等效。

!= ALL 或 <> ALL 运算符与NOT IN 等效。

2. 使用[NOT] EXISTS的子查询

如果子查询返回任何行,EXISTS将返回TRUE; 否则为FALSE.

多表更新

UPDATE table_references

SET col_name1 = {expr1 | DEFAULT}

[,col_name2 = {expr2 |DEFAULT}]…

[WHERE where_condition]

CREATE… SELECT

创建数据表同时将查询结果写入到数据表

CREATE TABLE [IF NOT EXISTS] tb_name

[(create_definition,…)]

Select_statement

连接

使用ON关键字来设定连接的条件,也可以使用WHERE来代替。

通常使用ON关键字来设定连接条件,

使用WHERE关键字进行结果集记录的过滤。

内连接

显示左表及右表符合连接条件的记录

\

左外连接

显示左表的全部记录及右表符合连接条件的记录

\

自身连接

数据表连接自己。

例:

1.子查询是指在另一个查询语句中的SELECT子句。

例句:

SELECT * FROM t1 WHERE column1 = (SELECTcolumn1 FROM t2);

其中,SELECT * FROM t1...称为Outer Query[外查询](或者Outer Statement),

SELECT column1 FROM t2 称为Sub Query[子查询]。

所以,我们说子查询是嵌套在外查询内部。而事实上它有可能在子查询内部再嵌套子查询。

子查询必须出现在圆括号之间。

行级子查询

SELECT * FROM t1 WHERE (col1,col2) = (SELECTcol3, col4 FROM t2 WHERE id = 10);

SELECT * FROM t1 WHERE ROW(col1,col2) =(SELECT col3, col4 FROM t2 WHERE id = 10);

行级子查询的返回结果最多为一行。

优化子查询

-- 创建数据表

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 NULLDEFAULT 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(&#39;R510VC15.6英寸笔记本&#39;,&#39;笔记本&#39;,&#39;华硕&#39;,&#39;3399&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;Y400N14.0英寸笔记本电脑&#39;,&#39;笔记本&#39;,&#39;联想&#39;,&#39;4899&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)VALUES(&#39;G150TH 15.6英寸游戏本&#39;,&#39;游戏本&#39;,&#39;雷神&#39;,&#39;8499&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)VALUES(&#39;X550CC 15.6英寸笔记本&#39;,&#39;笔记本&#39;,&#39;华硕&#39;,&#39;2799&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)VALUES(&#39;X240(20ALA0EYCD) 12.5英寸超极本&#39;,&#39;超级本&#39;,&#39;联想&#39;,&#39;4999&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;U330P13.3英寸超极本&#39;,&#39;超级本&#39;,&#39;联想&#39;,&#39;4299&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)VALUES(&#39;SVP13226SCB 13.3英寸触控超极本&#39;,&#39;超级本&#39;,&#39;索尼&#39;,&#39;7999&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;iPadmini MD531CH/A 7.9英寸平板电脑&#39;,&#39;平板电脑&#39;,&#39;苹果&#39;,&#39;1998&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;iPadAir MD788CH/A 9.7英寸平板电脑(16G WiFi版)&#39;,&#39;平板电脑&#39;,&#39;苹果&#39;,&#39;3388&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39; iPadmini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑(16G WiFi版)&#39;,&#39;平板电脑&#39;,&#39;苹果&#39;,&#39;2788&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)VALUES(&#39;IdeaCentre C340 20英寸一体电脑 &#39;,&#39;台式机&#39;,&#39;联想&#39;,&#39;3499&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)VALUES(&#39;Vostro 3800-R1206 台式电脑&#39;,&#39;台式机&#39;,&#39;戴尔&#39;,&#39;2899&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;iMacME086CH/A 21.5英寸一体电脑&#39;,&#39;台式机&#39;,&#39;苹果&#39;,&#39;9188&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)VALUES(&#39;AT7-7414LP 台式电脑(i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )&#39;,&#39;台式机&#39;,&#39;宏碁&#39;,&#39;3699&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)VALUES(&#39;Z220SFF F4F06PA工作站&#39;,&#39;服务器/工作站&#39;,&#39;惠普&#39;,&#39;4288&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)VALUES(&#39;PowerEdge T110 II服务器&#39;,&#39;服务器/工作站&#39;,&#39;戴尔&#39;,&#39;5388&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;MacPro MD878CH/A 专业级台式电脑&#39;,&#39;服务器/工作站&#39;,&#39;苹果&#39;,&#39;28888&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;HMZ-T3W 头戴显示设备&#39;,&#39;笔记本配件&#39;,&#39;索尼&#39;,&#39;6999&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;商务双肩背包&#39;,&#39;笔记本配件&#39;,&#39;索尼&#39;,&#39;99&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;X3250M4机架式服务器 2583i14&#39;,&#39;服务器/工作站&#39;,&#39;IBM&#39;,&#39;6888&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;玄龙精英版 笔记本散热器&#39;,&#39;笔记本配件&#39;,&#39;九州风神&#39;,&#39;&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;HMZ-T3W 头戴显示设备&#39;,&#39;笔记本配件&#39;,&#39;索尼&#39;,&#39;6999&#39;,DEFAULT,DEFAULT);

INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(&#39;商务双肩背包&#39;,&#39;笔记本配件&#39;,&#39;索尼&#39;,&#39;99&#39;,DEFAULT,DEFAULT);

-- 求所有电脑产品的平均价格,并且保留两位小数,AVG,MAX,MIN、COUNT、SUM为聚合函数

SELECT ROUND(AVG(goods_price),2) ASavg_price FROM tdb_goods;

-- 查询所有价格大于平均价格的商品,并且按价格降序排序

SELECT goods_id,goods_name,goods_price FROMtdb_goods WHERE goods_price > 5845.10 ORDER BY goods_price DESC;

-- 使用子查询来实现

SELECT goods_id,goods_name,goods_price FROMtdb_goods

WHERE goods_price > (SELECTROUND(AVG(goods_price),2) AS avg_price FROM tdb_goods)

ORDER BY goods_price DESC;

-- 查询类型为“超记本”的商品价格

SELECT goods_price FROM tdb_goods WHEREgoods_cate = &#39;超级本&#39;;

-- 查询价格大于或等于"超级本"价格的商品,并且按价格降序排列

SELECT goods_id,goods_name,goods_price FROMtdb_goods

WHERE goods_price = ANY(SELECT goods_priceFROM tdb_goods WHERE goods_cate = &#39;超级本&#39;)

ORDER BY goods_price DESC;

-- = ANY或 = SOME 等价于 IN

SELECT goods_id,goods_name,goods_price FROMtdb_goods

WHERE goods_price IN (SELECT goods_priceFROM tdb_goods WHERE goods_cate = &#39;超级本&#39;)

ORDER BY goods_price DESC;

-- 创建“商品分类”表

CREATE TABLE IF NOT EXISTS tdb_goods_cates(

cate_id SMALLINT UNSIGNED PRIMARY KEYAUTO_INCREMENT,

cate_name VARCHAR(40)

);

-- 查询tdb_goods表的所有记录,并且按"类别"分组

SELECT goods_cate FROM tdb_goods GROUP BYgoods_cate;

-- 将分组结果写入到tdb_goods_cates数据表

INSERT tdb_goods_cates (cate_name) SELECTgoods_cate FROM tdb_goods GROUP BY goods_cate;

-- 通过tdb_goods_cates数据表来更新tdb_goods表

UPDATE tdb_goods INNER JOIN tdb_goods_catesON goods_cate = cate_name

SET goods_cate = cate_id ;

-- 通过CREATE...SELECT来创建数据表并且同时写入记录

-- SELECT brand_name FROM tdb_goods GROUP BYbrand_name;

CREATE TABLE tdb_goods_brands (

brand_id SMALLINT UNSIGNED PRIMARY KEYAUTO_INCREMENT,

brand_name VARCHAR(40) NOT NULL

) SELECT brand_name FROM tdb_goods GROUP BYbrand_name;

-- 通过tdb_goods_brands数据表来更新tdb_goods数据表(错误)

UPDATE tdb_goods INNER JOIN tdb_goods_brands ON brand_name =brand_name

SET brand_name = brand_id;

-- Column &#39;brand_name&#39; in field list isambigous

-- 正确

UPDATE tdb_goods AS g INNER JOIN tdb_goods_brands AS b ON g.brand_name = b.brand_name

SET g.brand_name = b.brand_id;

-- 查看tdb_goods的数据表结构

DESC tdb_goods;

-- 通过ALTER TABLE语句修改数据表结构

ALTER TABLE tdb_goods

CHANGE goods_cate cate_id SMALLINT UNSIGNEDNOT NULL,

CHANGE brand_name brand_id SMALLINT UNSIGNEDNOT NULL;

-- 分别在tdb_goods_cates和tdb_goods_brands表插入记录

INSERT tdb_goods_cates(cate_name) VALUES(&#39;路由器&#39;),(&#39;交换机&#39;),(&#39;网卡&#39;);

INSERT tdb_goods_brands(brand_name) VALUES(&#39;海尔&#39;),(&#39;清华同方&#39;),(&#39;神舟&#39;);

-- 在tdb_goods数据表写入任意记录

INSERTtdb_goods(goods_name,cate_id,brand_id,goods_price) VALUES(&#39; LaserJet ProP1606dn 黑白激光打印机&#39;,&#39;12&#39;,&#39;4&#39;,&#39;1849&#39;);

-- 查询所有商品的详细信息(通过内连接实现)

SELECTgoods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods AS g

INNER JOIN tdb_goods_cates AS c ON g.cate_id= c.cate_id

INNER JOIN tdb_goods_brands AS b ONg.brand_id = b.brand_id\G;

-- 查询所有商品的详细信息(通过左外连接实现)

SELECT goods_id,goods_name,cate_name,brand_name,goods_priceFROM tdb_goods AS g

LEFT JOIN tdb_goods_cates AS c ON g.cate_id= c.cate_id

LEFT JOIN tdb_goods_brands AS b ONg.brand_id = b.brand_id\G;

-- 查询所有商品的详细信息(通过右外连接实现)

SELECT goods_id,goods_name,cate_name,brand_name,goods_priceFROM tdb_goods AS g

RIGHT JOIN tdb_goods_cates AS c ON g.cate_id= c.cate_id

RIGHT JOIN tdb_goods_brands AS b ONg.brand_id = b.brand_id\G;

-- 无限分类的数据表设计

CREATE TABLE tdb_goods_types(

type_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,

type_name VARCHAR(20) NOT NULL,

parent_id SMALLINT UNSIGNED NOT NULLDEFAULT 0

);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;家用电器&#39;,DEFAULT);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;电脑、办公&#39;,DEFAULT);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;大家电&#39;,1);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;生活电器&#39;,1);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;平板电视&#39;,3);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;空调&#39;,3);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;电风扇&#39;,4);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;饮水机&#39;,4);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;电脑整机&#39;,2);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;电脑配件&#39;,2);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;笔记本&#39;,9);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;超级本&#39;,9);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;游戏本&#39;,9);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;CPU&#39;,10);

INSERT tdb_goods_types(type_name,parent_id)VALUES(&#39;主机&#39;,10);

-- 查找所有分类及其父类

SELECTs.type_id,s.type_name,p.type_name

FROMtdb_goods_types AS s LEFT JOIN tdb_goods_types AS p

ONs.parent_id = p.type_id;

-- 查找所有分类及其子类

SELECTp.type_id,p.type_name,s.type_name

FROMtdb_goods_types AS p LEFT JOIN tdb_goods_types AS s

ONs.parent_id = p.type_id;

-- 查找所有分类及其子类的数目

SELECTp.type_id,p.type_name,count(s.type_name) AS children_count

FROMtdb_goods_types AS p LEFT JOIN tdb_goods_types AS s

ONs.parent_id = p.type_id

GROUP BYp.type_name

ORDER BYp.type_id;

-- 为tdb_goods_types添加child_count字段

ALTERTABLE tdb_goods_types ADD child_count MEDIUMINT UNSIGNED NOT NULL DEFAULT 0;

-- 将刚才查询到的子类数量更新到tdb_goods_types数据表

UPDATEtdb_goods_types AS t1 INNER JOIN

( SELECTp.type_id,p.type_name,count(s.type_name) AS children_count

FROMtdb_goods_types AS p

LEFTJOIN tdb_goods_types AS s ON s.parent_id = p.type_id

GROUP BYp.type_name

ORDER BYp.type_id ) AS t2

ON t1.type_id = t2.type_id

SETt1.child_count = t2.children_count;

-- 复制编号为12,20的两条记录

SELECT * FROM tdb_goods WHERE goods_id IN(19,20);

--INSERT ... SELECT实现复制

INSERTtdb_goods(goods_name,cate_id,brand_id)

SELECTgoods_name,cate_id,brand_id

FROMtdb_goods

WHEREgoods_id IN (19,20);

-- 查找重复记录

SELECTgoods_id,goods_name

FROMtdb_goods

GROUP BYgoods_name

HAVINGcount(goods_name) >= 2;

-- 删除重复记录

DELETEt1 FROM tdb_goods AS t1

LEFTJOIN (SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name

HAVINGcount(goods_name) >= 2 ) AS t2

ONt1.goods_name = t2.goods_name

WHEREt1.goods_id > t2.goods_id;

相关文章
最新文章
热点推荐