又是周六了-MySQL特训

  hi

又是周六,又是磨蹭个一上午~午饭后开始吧

1、MySQL

-----子查询与连接(三)-----

----使用INSERT...SELECT插入记录

--数据库内容的英文版本

由于我的WAMP中的MYSQL控制台显示中文无力,在更改过程中心力交瘁,遂改变战术,把数据改为了全英文的,现分享出来

-- 创建数据表

CREATE TABLE IF NOT EXISTS 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 goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘R510VC 15.6Inches Laptop‘,‘Laptop‘,‘ASUS‘,‘3399‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘Y400N 14.0Inches Laptop‘,‘Laptop‘,‘Lenovo‘,‘4899‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘G150TH 15.6Inches Gamebook‘,‘Gamebook‘,‘ThunderGod‘,‘8499‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘X550CC 15.6Inches Laptop‘,‘Laptop‘,‘ASUS‘,‘2799‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘X240(20ALA0EYCD) 12.5InchesUltrabook‘,‘Ultrabook‘,‘Lenovo‘,‘4999‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘U330P 13.3Inches Ultrabook‘,‘Ultrabook‘,‘Lenovo‘,‘4299‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘SVP13226SCB 13.3Inches Touch Ultrabook‘,‘Ultrabook‘,‘Sony‘,‘7999‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘iPad mini MD531CH/A 7.9Inches Tablet‘,‘Tablet‘,‘Apple‘,‘1998‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘iPad Air MD788CH/A 9.7Inches Tablet (16G WiFi Vision)‘,‘Tablet‘,‘Apple‘,‘3388‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘ iPad mini ME279CH/A with Retina screen 7.9InchesTablet (16G WiFi Vision)‘,‘Tablet‘,‘Apple‘,‘2788‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘IdeaCentre C340 20Inches All-in-one ‘,‘Desktop‘,‘Lenovo‘,‘3499‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘Vostro 3800-R1206 desktop‘,‘Desktop‘,‘Dell‘,‘2899‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘iMac ME086CH/A 21.5Inches All-in-one‘,‘Desktop‘,‘Apple‘,‘9188‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘AT7-7414LP Desktop (i5-3450 4G 500G 2GIndependent graphics card DVD Keyboard&Mouse Linux )‘,‘Desktop‘,‘Acer‘,‘3699‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘Z220SFF F4F06PAWork station‘,‘server/Work station‘,‘HP‘,‘4288‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘PowerEdge T110 IIserver‘,‘server/Work station‘,‘Dell‘,‘5388‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘Mac Pro MD878CH/A Professional Desktop‘,‘server/Work station‘,‘Apple‘,‘28888‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘ HMZ-T3W Headset Display Device‘,‘Laptop accessory‘,‘Sony‘,‘6999‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘Business Backpage‘,‘Laptopaccessory‘,‘Sony‘,‘99‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘X3250 M4 server 2583i14‘,‘server/Work station‘,‘IBM‘,‘6888‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘Xuanlong Laptop Radiator‘,‘Laptop accessory‘,‘Windgod‘,‘‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘ HMZ-T3W Headset Display Device‘,‘Laptop accessory‘,‘Sony‘,‘6999‘,DEFAULT,DEFAULT);

INSERT goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(‘Business Backpage‘,‘Laptop accessory‘,‘Sony‘,‘99‘,DEFAULT,DEFAULT);

---INSERT...SELECT

INSERT [INTO] tbl_name [(col_name)] SELECT

使用的时候就这样,创建一个新的表

CREATE TABLE IF NOT EXISTS cates(

cate_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,

cate_name VARCHAR(40) NOT NULL

);

然后把原数据表中的东西有选择性的插入进来

INSERT INTO CATES(CATE_NAME) SELECT GOODS_CATE FROM GOODS GROUP BY GOODS_CATE;

如果需要根据目录表cates,来更新我们的参数表——多表更新

----多表更新

--多步

UPDATE table_ref SET col_name=... ...

其中,ref是表的参照关系

这里有连接关系,INNER JOIN LEFT JOIN 等

最简单的是INNER,内连接

mysql> UPDATE GOODS INNER JOIN CATES ON GOODS_CATE=CATE_NAME
-> SET GOODS_CATE=CATE_ID;

(请原谅我不想打小写的了。。)

解释一下:更新goods这张表,内连接cates这张表,在什么条件呢——goods_cate=cate_name,设定的值是什么呢,是cates这张表中的cate_id

mysql> SELECT * FROM GOODS\G; 验证一下

--一步

CREATE...SELECT...

即创建表的同时写入连接的数据

这里的例子是:对品牌做连接更新

mysql> SELECT BRAND_NAME FROM GOODS GROUP BY BRAND_NAME;

mysql> CREATE TABLE IF NOT EXISTS brands(
-> brand_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> brand_name VARCHAR(40) NOT NULL
-> )
-> SELECT BRAND_NAME FROM GOODS GROUP BY BRAND_NAME;

就可以省去INSERT的操作了

但是这时候更新要出错

mysql> UPDATE GOODS INNER JOIN BRANDS ON BRAND_NAME=BRAND_NAME
-> SET BRAND_NAME=BRAND_ID;
ERROR 1052 (23000): Champ: ‘BRAND_NAME‘ dans field list est ambigu

因为名字一样,要改字段,或者起别名AS

mysql> UPDATE GOODS AS G INNER JOIN BRANDS AS B ON G.BRAND_NAME=B.BRAND_NAME
-> SET G.BRAND_NAME=BRAND_ID;

虽然实现了外表的更新,但是,比如说goods_cate的类型还是varchar,而我们其实希望主表中,它是int类型的。这时候由于需要修改字段名以及数据类型,用ALTER CHANGE

mysql> ALTER TABLE GOODS
-> CHANGE GOODS_CATE CATE_ID SMALLINT UNSIGNED NOT NULL,
-> CHANGE BRAND_NAME BRAND_ID SMALLINT UNSIGNED NOT NULL;

这种外键称为事实外键,比foreign key的物理外键还要多

----连接

由于我们实现了多表的存储,但是由于显示等需求,要求多个表联动,需要用到连接

数据表参照ref:tbl_name [[AS] alias] | table_subquery [AS] alias

---INNER JOIN

使用ON来设定连接条件,WHERE一般做进一步的筛选

内连接,仅显示符合连接条件的记录

SELECT GOODS_ID,GOODS_NAME,CATE_NAME FROM GOODS INNER JOIN CATES ON GOODS.CATE_ID=CATES.CATE_ID;

翻译一下:选取goods表中的goods_id,goods_name,cate_name字段,但是显示的时候,需要内连接cates表(因为我们不想显示的是类型的数字表示),条件是当两者的cate_id字段相等;这里由于cate_id没有给别名,所以写成表名.cate_id

会发现,这个内连接的显示,只显示了on条件的结果

---外连接

外连接又分为左外和右外

LEFT JOIN显示左表的全部记录和右表符合链接条件的记录,例子如下

+----------+--------------------------------------------------------------------------------------------+---------------------+
| GOODS_ID | GOODS_NAME | CATE_NAME |
+----------+--------------------------------------------------------------------------------------------+---------------------+
| 1 | R510VC 15.6Inches Laptop | Laptop |
| 2 | Y400N 14.0Inches Laptop | Laptop |
| 3 | G150TH 15.6Inches Gamebook | Gamebook |
| 4 | X550CC 15.6Inches Laptop | Laptop |
| 5 | X240(20ALA0EYCD) 12.5InchesUltrabook | Ultrabook |
| 6 | U330P 13.3Inches Ultrabook | Ultrabook |
| 7 | SVP13226SCB 13.3Inches Touch Ultrabook | Ultrabook |
| 8 | iPad mini MD531CH/A 7.9Inches Tablet | Tablet |
| 9 | iPad Air MD788CH/A 9.7Inches Tablet ?16G WiFi Vision? | Tablet |
| 10 | iPad mini ME279CH/A with Retina screen 7.9InchesTablet ?16G WiFi Vision? | Tablet |
| 11 | IdeaCentre C340 20Inches All-in-one | Desktop |
| 12 | Vostro 3800-R1206 desktop | Desktop |
| 13 | iMac ME086CH/A 21.5Inches All-in-one | Desktop |
| 14 | AT7-7414LP Desktop ?i5-3450 4G 500G 2GIndependent graphics card DVD Keyboard&Mouse Linux ? | Desktop |
| 15 | Z220SFF F4F06PAWork station | server/Work station |
| 16 | PowerEdge T110 IIserver | server/Work station |
| 17 | Mac Pro MD878CH/A Professional Desktop | server/Work station |
| 18 | HMZ-T3W Headset Display Device | Laptop accessory |
| 19 | Business Backpage | Laptop accessory |
| 20 | X3250 M4 server 2583i14 | server/Work station |
| 21 | Xuanlong Laptop Radiator | Laptop accessory |
| 22 | HMZ-T3W Headset Display Device | Laptop accessory |
| 23 | Business Backpage | Laptop accessory |
+----------+--------------------------------------------------------------------------------------------+---------------------+
23 rows in set (0.00 sec)

mysql> SELECT GOODS_ID,GOODS_NAME,CATE_NAME FROM GOODS LEFT JOIN CATES ON GOODS.CATE_ID=CATES.CATE_ID;
+----------+--------------------------------------------------------------------------------------------+---------------------+
| GOODS_ID | GOODS_NAME | CATE_NAME |
+----------+--------------------------------------------------------------------------------------------+---------------------+
| 1 | R510VC 15.6Inches Laptop | Laptop |
| 2 | Y400N 14.0Inches Laptop | Laptop |
| 3 | G150TH 15.6Inches Gamebook | Gamebook |
| 4 | X550CC 15.6Inches Laptop | Laptop |
| 5 | X240(20ALA0EYCD) 12.5InchesUltrabook | Ultrabook |
| 6 | U330P 13.3Inches Ultrabook | Ultrabook |
| 7 | SVP13226SCB 13.3Inches Touch Ultrabook | Ultrabook |
| 8 | iPad mini MD531CH/A 7.9Inches Tablet | Tablet |
| 9 | iPad Air MD788CH/A 9.7Inches Tablet ?16G WiFi Vision? | Tablet |
| 10 | iPad mini ME279CH/A with Retina screen 7.9InchesTablet ?16G WiFi Vision? | Tablet |
| 11 | IdeaCentre C340 20Inches All-in-one | Desktop |
| 12 | Vostro 3800-R1206 desktop | Desktop |
| 13 | iMac ME086CH/A 21.5Inches All-in-one | Desktop |
| 14 | AT7-7414LP Desktop ?i5-3450 4G 500G 2GIndependent graphics card DVD Keyboard&Mouse Linux ? | Desktop |
| 15 | Z220SFF F4F06PAWork station | server/Work station |
| 16 | PowerEdge T110 IIserver | server/Work station |
| 17 | Mac Pro MD878CH/A Professional Desktop | server/Work station |
| 18 | HMZ-T3W Headset Display Device | Laptop accessory |
| 19 | Business Backpage | Laptop accessory |
| 20 | X3250 M4 server 2583i14 | server/Work station |
| 21 | Xuanlong Laptop Radiator | Laptop accessory |
| 22 | HMZ-T3W Headset Display Device | Laptop accessory |
| 23 | Business Backpage | Laptop accessory |
| 24 | LaserJet Pro P1606dn Black&White blazer printer | NULL |
+----------+--------------------------------------------------------------------------------------------+---------------------+
24 rows in set (0.00 sec)

注意最后一个记录的类名是null

RIGHT JOIN是这样的

mysql> SELECT GOODS_ID,GOODS_NAME,CATE_NAME FROM GOODS RIGHT JOIN CATES ON GOODS.CATE_ID=CATES.CATE_ID;
+----------+--------------------------------------------------------------------------------------------+---------------------+
| GOODS_ID | GOODS_NAME | CATE_NAME |
+----------+--------------------------------------------------------------------------------------------+---------------------+
| 1 | R510VC 15.6Inches Laptop | Laptop |
| 2 | Y400N 14.0Inches Laptop | Laptop |
| 3 | G150TH 15.6Inches Gamebook | Gamebook |
| 4 | X550CC 15.6Inches Laptop | Laptop |
| 5 | X240(20ALA0EYCD) 12.5InchesUltrabook | Ultrabook |
| 6 | U330P 13.3Inches Ultrabook | Ultrabook |
| 7 | SVP13226SCB 13.3Inches Touch Ultrabook | Ultrabook |
| 8 | iPad mini MD531CH/A 7.9Inches Tablet | Tablet |
| 9 | iPad Air MD788CH/A 9.7Inches Tablet ?16G WiFi Vision? | Tablet |
| 10 | iPad mini ME279CH/A with Retina screen 7.9InchesTablet ?16G WiFi Vision? | Tablet |
| 11 | IdeaCentre C340 20Inches All-in-one | Desktop |
| 12 | Vostro 3800-R1206 desktop | Desktop |
| 13 | iMac ME086CH/A 21.5Inches All-in-one | Desktop |
| 14 | AT7-7414LP Desktop ?i5-3450 4G 500G 2GIndependent graphics card DVD Keyboard&Mouse Linux ? | Desktop |
| 15 | Z220SFF F4F06PAWork station | server/Work station |
| 16 | PowerEdge T110 IIserver | server/Work station |
| 17 | Mac Pro MD878CH/A Professional Desktop | server/Work station |
| 18 | HMZ-T3W Headset Display Device | Laptop accessory |
| 19 | Business Backpage | Laptop accessory |
| 20 | X3250 M4 server 2583i14 | server/Work station |
| 21 | Xuanlong Laptop Radiator | Laptop accessory |
| 22 | HMZ-T3W Headset Display Device | Laptop accessory |
| 23 | Business Backpage | Laptop accessory |
| NULL | NULL | Adaptor |
| NULL | NULL | Exchanger |
| NULL | NULL | Netcard |
+----------+--------------------------------------------------------------------------------------------+---------------------+
26 rows in set (0.00 sec)

----多表连接

mysql> SELECT GOODS_ID,GOODS_NAME,CATE_NAME,BRAND_NAME,GOODS_PRICE FROM GOODS AS G
-> INNER JOIN CATES AS C ON G.CATE_ID=C.CATE_ID
-> INNER JOIN BRANDS AS B ON G.BRAND_ID=B.BRAND_ID;

每连接一个表就需要一个JOIN,注意起别名给数据表!

此时的显示虽然跟一开始一样,但实现不同,意义大不一样

----无限级分类表设计

一般实际中会出现无限级数据表,不断往下分

举个例子

CREATE TABLE 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 goods_types(type_name,parent_id) VALUES(‘Home Electronics‘,DEFAULT);
INSERT goods_types(type_name,parent_id) VALUES(‘PC、Office‘,DEFAULT);
INSERT goods_types(type_name,parent_id) VALUES(‘Appliances‘,1);
INSERT goods_types(type_name,parent_id) VALUES(‘Life Appliances‘,1);
INSERT goods_types(type_name,parent_id) VALUES(‘Tablet TV‘,3);
INSERT goods_types(type_name,parent_id) VALUES(‘Air Conditioner‘,3);
INSERT goods_types(type_name,parent_id) VALUES(‘Electrical Fan‘,4);
INSERT goods_types(type_name,parent_id) VALUES(‘Water Dispenser‘,4);
INSERT goods_types(type_name,parent_id) VALUES(‘Complete Machine‘,2);
INSERT goods_types(type_name,parent_id) VALUES(‘Accessory‘,2);
INSERT goods_types(type_name,parent_id) VALUES(‘Laptop‘,9);
INSERT goods_types(type_name,parent_id) VALUES(‘Ultrabook‘,9);
INSERT goods_types(type_name,parent_id) VALUES(‘Gamebook‘,9);
INSERT goods_types(type_name,parent_id) VALUES(‘CPU‘,10);
INSERT goods_types(type_name,parent_id) VALUES(‘Host Computer‘,10);

mysql> SELECT * FROM GOODS_TYPES;
+---------+------------------+-----------+
| type_id | type_name | parent_id |
+---------+------------------+-----------+
| 1 | Home Electronics | 0 |
| 2 | PC?Office | 0 |
| 3 | Appliances | 1 |
| 4 | Life Appliances | 1 |
| 5 | Tablet TV | 3 |
| 6 | Air Conditioner | 3 |
| 7 | Electrical Fan | 4 |
| 8 | Water Dispenser | 4 |
| 9 | Complete Machine | 2 |
| 10 | Accessory | 2 |
| 11 | Laptop | 9 |
| 12 | Ultrabook | 9 |
| 13 | Gamebook | 9 |
| 14 | CPU | 10 |
| 15 | Host Computer | 10 |
+---------+------------------+-----------+

注意parent_id,是指改节点的级别,比如0,表示home electronics没有父节点,是个顶级节点

查找的实现,是通过自身连接来完成的

---自身连接

举个例子,显示所有类别的父类是什么

这里,自身的连接,可以想象右边有一张一模一样的表(左边也可以,有些东西稍微变一下就行)

mysql> SELECT S.TYPE_ID,S.TYPE_NAME,P.TYPE_NAME FROM GOODS_TYPES AS S LEFT JOIN GOODS_TYPES AS P
-> ON S.PARENT_ID=P.TYPE_ID;
+---------+------------------+------------------+
| TYPE_ID | TYPE_NAME | TYPE_NAME |
+---------+------------------+------------------+
| 1 | Home Electronics | NULL |
| 2 | PC?Office | NULL |
| 3 | Appliances | Home Electronics |
| 4 | Life Appliances | Home Electronics |
| 5 | Tablet TV | Appliances |
| 6 | Air Conditioner | Appliances |
| 7 | Electrical Fan | Life Appliances |
| 8 | Water Dispenser | Life Appliances |
| 9 | Complete Machine | PC?Office |
| 10 | Accessory | PC?Office |
| 11 | Laptop | Complete Machine |
| 12 | Ultrabook | Complete Machine |
| 13 | Gamebook | Complete Machine |
| 14 | CPU | Accessory |
| 15 | Host Computer | Accessory |
+---------+------------------+------------------+

翻译一下,就是,可以先从条件说起,当子表s中的parent_id=父表中的type_id时,选取子表的type_id,type_name,以及父表的type_name显示,这里的别名可以在语句的末尾处显示

要把这个逻辑关系先说说/想想清楚,再翻译为sql语句

反过来,查找所有类别的子类

先考虑逻辑关系:当父表的type_id=子表的parent_id时,取 P.TYPE_ID,P.TYPE_NAME,S.TYPE_NAME ——再写出语句

mysql> SELECT P.TYPE_ID,P.TYPE_NAME,S.TYPE_NAME FROM GOODS_TYPES AS P LEFT JOIN GOODS_TYPES AS S
-> ON P.TYPE_ID=S.PARENT_ID;
+---------+------------------+------------------+
| TYPE_ID | TYPE_NAME | TYPE_NAME |
+---------+------------------+------------------+
| 1 | Home Electronics | Appliances |
| 1 | Home Electronics | Life Appliances |
| 3 | Appliances | Tablet TV |
| 3 | Appliances | Air Conditioner |
| 4 | Life Appliances | Electrical Fan |
| 4 | Life Appliances | Water Dispenser |
| 2 | PC?Office | Complete Machine |
| 2 | PC?Office | Accessory |
| 9 | Complete Machine | Laptop |
| 9 | Complete Machine | Ultrabook |
| 9 | Complete Machine | Gamebook |
| 10 | Accessory | CPU |
| 10 | Accessory | Host Computer |
| 5 | Tablet TV | NULL |
| 6 | Air Conditioner | NULL |
| 7 | Electrical Fan | NULL |
| 8 | Water Dispenser | NULL |
| 11 | Laptop | NULL |
| 12 | Ultrabook | NULL |
| 13 | Gamebook | NULL |
| 14 | CPU | NULL |
| 15 | Host Computer | NULL |
+---------+------------------+------------------+

再复杂一点,且留给你们自己翻译

mysql> SELECT P.TYPE_ID,P.TYPE_NAME,COUNT(S.TYPE_NAME) CHILD_COUNT FROM GOODS_TYPES AS P LEFT JOIN GOODS_TYPES AS S
-> ON P.TYPE_ID=S.PARENT_ID GROUP BY P.TYPE_NAME ORDER BY P.TYPE_ID;
+---------+------------------+-------------+
| TYPE_ID | TYPE_NAME | CHILD_COUNT |
+---------+------------------+-------------+
| 1 | Home Electronics | 2 |
| 2 | PC?Office | 2 |
| 3 | Appliances | 2 |
| 4 | Life Appliances | 2 |
| 5 | Tablet TV | 0 |
| 6 | Air Conditioner | 0 |
| 7 | Electrical Fan | 0 |
| 8 | Water Dispenser | 0 |
| 9 | Complete Machine | 3 |
| 10 | Accessory | 2 |
| 11 | Laptop | 0 |
| 12 | Ultrabook | 0 |
| 13 | Gamebook | 0 |
| 14 | CPU | 0 |
| 15 | Host Computer | 0 |
+---------+------------------+-------------+

----多表删除

基本思路也是通过一张表模拟多张表的操作

懒了,直接复制看吧,需要了再去做

-- INSERT ... SELECT实现复制

INSERT goods(goods_name,cate_id,brand_id) SELECT goods_name,cate_id,brand_id FROM goods WHERE goods_id IN (19,20);

-- 查找重复记录

SELECT goods_id,goods_name FROM goods GROUP BY goods_name HAVING count(goods_name) >= 2;

-- 删除重复记录

DELETE t1 FROM goods AS t1 LEFT JOIN (SELECT goods_id,goods_name FROM goods GROUP BY goods_name HAVING count(goods_name) >= 2 ) AS t2 ON t1.goods_name = t2.goods_name WHERE t1.goods_id > t2.goods_id;

时间: 2024-09-30 18:41:20

又是周六了-MySQL特训的相关文章

Mysql实训任务书

注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6635189537079296526/ 什么是数据库:数据库(Database)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. 简单来说是本身可视为电子化的文件柜--存储电子文件的处所,用户可以对文件中的数据进行新增.截取.更新.删除等操作. 在经济管理的日常工作中,常常需要把某些相关的数据放进这样的"仓库",并根据管理的需要进行相应的处理. 严格来说,数据库是长期储存在计算机内.

MySQL(5.6) 函数

字符串函数 ASCII(str) 说明:返回字符串 str 最左边字符的 ASCII 值 mysql> SELECT ASCII('2'); -> 50 mysql> SELECT ASCII(2); -> 50 mysql> SELECT ASCII('a'); -> 97 mysql> SELECT ASCII(''); -> 0 mysql> SELECT ASCII(NULL); -> NULL BIN(N) 说明:返回数值 N 的二进制

百亿互金平台救火故事

多年前,又是周六客服打电话过来,平台官网不能访问,app完全无法打开,客户在QQ群和微信群中各种反馈,说平台是不是跑路了?客服的多条400热线完全被打爆,电话已经接不过来- 前言 一直以来总是想以什么方式去记录下自己在互金行业的这段经历,趁着自己还记得清楚,还能找到一些资料原型,一方面可以分享出来供大家参考,但是更重要就是多年以后我可以根据这些文章回忆起来自己的那段激情岁月. 想了很久但一直没有实施,后来觉得应该从架构的角度来梳理一篇文章,就写了从零到百亿互联网金融架构发展史这篇文章:最后认为只

SSH系列:(28)JDK Timer和Quartz

常见的任务调度有Jdk 的Timer 以及 spring中quartz任务调度框架等. 1.JDK Timer 如果是执行简单的有一定执行周期的,那么使用jdk自带的timer是最简单的. 具体步骤: ①.编写一个简单类继承 TimerTask,在这个新编写的类中重写父类中run方法,在run中执行要执行的操作: ②.编写一个简单类,在类中写一个方法,方法体中使用timer调用在①中创建的类并设置好timer执行周期. MyTask.java package com.rk.test; impor

入职第二天(培训第二天)

又是一天培,训的结束,今天的培训内容非常丰富,我也学到了很多的东西(不得不感叹入职后快速学习能力的重要性).今天的培训分为三块:沟通技巧,时间管理,班级活动. 上午的9:30-12:00都是由田老师讲解沟通的技巧.田老师讲的第一点是职业发展.作为刚入职的信任,为自己做一个职业规划是非常重要的,一个好的职业规划能够引导你走向成功.首先,需要有一个对于成功的定义.对于我目前阶段而言,成功就是成就足够的物质基础,足够我在一个一线城市买套房,结婚,为父母也买套房子,一家人过上潇洒的生活.为了这个成功,我

[spring-framework]Spring定时器的配置和使用

开发中我们常常会做一些定时任务,这些任务有开始时间,并会按一定的周期或规则执行.如此我们在Java程序开发中使用定时器来处理定时任务. <!-- MessageRequestTask类中包含了msgRequest方法,用于执行定时任务 --> <bean id="msg_request" class="com.santorini.task.timer.MessageRequestTask"></bean> <!-- 定时器配

Spring定时器的配置和使用

开发中我们常常会做一些定时任务,这些任务有开始时间,并会按一定的周期或规则执行.如此我们在Java程序开发中使用定时器来处理定时任务. <!-- MessageRequestTask类中包含了msgRequest方法,用于执行定时任务 --> <bean id="msg_request" class="com.santorini.task.timer.MessageRequestTask"></bean> <!-- 定时器配

【SSH项目实战】国税协同平台-34.quartz&amp;CronTrigger

我们上次使用quartz任务调度框架创建了一个在后台按照时间间隔不停运行的任务,我们使用的是simpleTrigger简单触发器,为了实现我们的月末自动回复的功能,我们要设置触发器能够在某个精确时间去自动执行任务,那么使用simpleTrigger简单触发器就远远不够了,我们需要使用CronTrigger任务触发器来实现这个功能. 我们的CronTrigger设置步骤如下: 任务触发器(CronTrigger) ① 设置任务详细 ② 设置执行时机(cronExpression) cronExpr

Spring定时器的配置

Spring定时器的配置(注解+xml)方式 (2013-09-30 10:39:18)转载▼ 标签: spring定时器配置 spring定时器注解方式 spring定时器xml方式 spring配置 分类: Spring Spring 配置定时器(注解+xml)方式—整理 一.注解方式 1. 在Spring的配置文件ApplicationContext.xml,首先添加命名空间 xmlns:task="http://www.springframework.org/schema/task&qu