mysql登录 默认端口3306 127.0.0.1本地回环地址
登录数据库
mysql -uroot -p
数据库退出
mysql >exit;
mysql >quit;
mysql >\q;
创建数据库
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name
CREATE DATABASE t1;
查看当前服务器下的数据表列表
SHOW DATABASES;
修改数据库编码方式
ALTER DATABASE test CHARACTER SET utf8 COLLATE utf8_general_ci;
删除数据库
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
数据类型
整型
浮点型
日期类型
字符型
打开数据库
USE 数据库名称
显示当前打开的数据库
SELECT DATABASE();
创建数据表
CREATE TABLE [IF NOT EXISTS] table_name(
column_name data_type,
…
username VARCHAR(20),
age TINYINT UNSIGNED,
salary FLOAT(8,2) UNSIGNED
);
查看数据表
SHOW TABLES
SHOW TABLES [FROM db_name]
查看数据表结构
SHOW COLUMNS FROM tbl_name
INSERT
插入记录
INSERT [INTO] tbl_name[(col_name,…)] VALUES(val,…)
查找记录
SELECT expr,…FROM tbl_name
空值与非空
NULL 可以为空
NOT NULL 不可以为空
CREATE TABLE [IF NOT EXISTS] table_name(
column_name data_type,
…
username VARCHAR(20) NOT NULL,
age TINYINT UNSIGNED NULL,
salary FLOAT(8,2) UNSIGNED NULL
)
AUTO_INCREMENT
自动编号 且必须与主键组合使用
默认值1
递增1
PRIMARY KEY
主键约束
每张数据表只能存在一个主键
主键保存记录的唯一性
主键自动为NOT NULL
CREATE TABLE tb3(
id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
)
UNIQUE KEY
唯一约束
唯一约束可以保存记录的唯一性
唯一约束的字段可以为空值
每张数据表可以有多个
DEFAULT
默认值
CREATE TABLE tb(
id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(20) NOT NULL UNIQUE KEY,
sex ENUM(‘1’,’2’,’3’) DEFAULT ‘3’
)
约束
约束保证数据的完整性和一致性
约束分为表级约束和列级约束
约束类型包括
NOT NULL(非空约束)
PRIMARY kEY 主键约束
UNIQUE KEY 唯一约束
DEFAULT 默认约束
FOREIGN KEY外键约束
CREATE TABLE provinces(
id SMALLINT UNSIGNED PRIMARY kEY AUTO_INCREMENT,
username VARCHAR(10) NOT NULL
);
CREATE TABLE user(
id SMALLINT UNSIGNED PRIMARY KEY ATUO_INCREMENT,
pid SMALLINT UNSIGNED,
FOREIGN KEY (pid) REFERENCES provinces(id)
);
CREATE TABLE user1(
id
username
pid SMALLINT UNSIGNED,
FOREIGN KEY (pid) REFERENCES provinces (id) ON DELETE CASCADE
);
修改数据表
添加单列
ALTER TABE tbl_name ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]
添加多列
ALTER TABLE tbl_name ADD [COLUMN] (col_name column_definition,…)
删除列
ALTER TABLE tbl_name DROP [COLUMN] col_name
添加主键约束
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,…)
操作数据表中的记录
INSERT
插入记录
方法一 INSERT [INTO] tab_name [(col_name,…)] {VALUES | VALUE} ({expr | DEFAULT},…),(…),…
方法二 INSERT [INTO] tbl_name SET col_name={expr | DEFAULT},..
说明:与第一种方式的区别在于,此方法可以使用子查询(SubQuery)
方法三 INSERT [INTO] tbl_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 tbl_name [WHERE where_condition]
SELECT
查找记录
SELECT select_expr [, select_expr …]
[
FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | position} [ASC | DESC],…]
[HAVING where_condition]
[ORDER BY {col_name | expr | position} [ASC | DESC],…]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
]
WHERE
条件表达式
GROUP BY
查询结果分组
[GROUP BY {col_name | position} [ASC | DESC], …]
HAVING
分组条件
[HAVING where_condition]
SELECT sex,age FROM users GROUP BY 1 HAVING age >35
ORDER BY
对查询结果进行排序
[ORDER BY {col_name | expr | position} [ASC | DESC],…]
SELECT * FROM users ORDER BY id DESC;
LIMIT
限制查询结果返回的数量
[LIMIT {[offset,] },{position}]
SELECT * FROM users ORDER BY id DESC LIMIT 2,2;
INSERT
插入记录
INSERT [INTO] tbl_name SET col_name = {expr | DEFAULT},..
-- 创建数据表
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('玄龙精英版 笔记本散热器','笔记本配件','九州风神','',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);
-- 求所有电脑产品的平均价格,并且保留两位小数,AVG,MAX,MIN、COUNT、SUM为聚合函数
SELECT ROUND(AVG(goods_price),2) AS avg_price FROM tdb_goods;
-- 查询所有价格大于平均价格的商品,并且按价格降序排序
SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price > 5845.10 ORDER BY goods_price DESC;
-- 使用子查询来实现
SELECT goods_id,goods_name,goods_price FROM tdb_goods
WHERE goods_price > (SELECT ROUND(AVG(goods_price),2) AS avg_price FROM tdb_goods)
ORDER BY goods_price DESC;
-- 查询类型为“超记本”的商品价格
SELECT goods_price FROM tdb_goods WHERE goods_cate = '超级本';
-- 查询价格大于或等于"超级本"价格的商品,并且按价格降序排列
SELECT goods_id,goods_name,goods_price FROM tdb_goods
WHERE goods_price = ANY(SELECT goods_price FROM tdb_goods WHERE goods_cate = '超级本')
ORDER BY goods_price DESC;
-- = ANY 或 = SOME 等价于 IN
SELECT goods_id,goods_name,goods_price FROM tdb_goods
WHERE goods_price IN (SELECT goods_price FROM tdb_goods WHERE goods_cate = '超级本')
ORDER BY goods_price DESC;
-- 创建“商品分类”表
CREATE TABLE IF NOT EXISTS tdb_goods_cates(
cate_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
cate_name VARCHAR(40)
);
-- 查询tdb_goods表的所有记录,并且按"类别"分组
SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;
-- 将分组结果写入到tdb_goods_cates数据表
INSERT tdb_goods_cates (cate_name) SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;
-- 通过tdb_goods_cates数据表来更新tdb_goods表
UPDATE tdb_goods INNER JOIN tdb_goods_cates ON goods_cate = cate_name
SET goods_cate = cate_id ;
-- 通过CREATE...SELECT来创建数据表并且同时写入记录
-- SELECT brand_name FROM tdb_goods GROUP BY brand_name;
CREATE TABLE tdb_goods_brands (
brand_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
brand_name VARCHAR(40) NOT NULL
) SELECT brand_name FROM tdb_goods GROUP BY brand_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 'brand_name' in field list is ambigous
-- 正确
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 UNSIGNED NOT NULL,
CHANGE brand_name brand_id SMALLINT UNSIGNED NOT NULL;
-- 分别在tdb_goods_cates和tdb_goods_brands表插入记录
INSERT tdb_goods_cates(cate_name) VALUES('路由器'),('交换机'),('网卡');
INSERT tdb_goods_brands(brand_name) VALUES('海尔'),('清华同方'),('神舟');
-- 在tdb_goods数据表写入任意记录
INSERT tdb_goods(goods_name,cate_id,brand_id,goods_price) VALUES(' LaserJet Pro P1606dn 黑白激光打印机','12','4','1849');
-- 查询所有商品的详细信息(通过内连接实现)
SELECT goods_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 ON g.brand_id = b.brand_id\G;
-- 查询所有商品的详细信息(通过左外连接实现)
SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM 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 ON g.brand_id = b.brand_id\G;
-- 查询所有商品的详细信息(通过右外连接实现)
SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM 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 ON g.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 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 s.type_id,s.type_name,p.type_name FROM tdb_goods_types AS s LEFT JOIN tdb_goods_types AS p ON s.parent_id = p.type_id;
-- 查找所有分类及其子类
SELECT p.type_id,p.type_name,s.type_name FROM tdb_goods_types AS p LEFT JOIN tdb_goods_types AS s ON s.parent_id = p.type_id;
-- 查找所有分类及其子类的数目
SELECT p.type_id,p.type_name,count(s.type_name) AS children_count FROM tdb_goods_types AS p LEFT JOIN tdb_goods_types AS s ON s.parent_id = p.type_id GROUP BY p.type_name ORDER BY p.type_id;
-- 为tdb_goods_types添加child_count字段
ALTER TABLE tdb_goods_types ADD child_count MEDIUMINT UNSIGNED NOT NULL DEFAULT 0;
-- 将刚才查询到的子类数量更新到tdb_goods_types数据表
UPDATE tdb_goods_types AS t1 INNER JOIN ( SELECT p.type_id,p.type_name,count(s.type_name) AS children_count FROM tdb_goods_types AS p
LEFT JOIN tdb_goods_types AS s ON s.parent_id = p.type_id
GROUP BY p.type_name
ORDER BY p.type_id ) AS t2
ON t1.type_id = t2.type_id
SET t1.child_count = t2.children_count;
-- 复制编号为12,20的两条记录
SELECT * FROM tdb_goods WHERE goods_id IN (19,20);
-- INSERT ... SELECT实现复制
INSERT tdb_goods(goods_name,cate_id,brand_id) SELECT goods_name,cate_id,brand_id FROM tdb_goods WHERE goods_id IN (19,20);
-- 查找重复记录
SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING count(goods_name) >= 2;
-- 删除重复记录
DELETE t1 FROM tdb_goods AS t1 LEFT JOIN (SELECT goods_id,goods_name FROM tdb_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;
子查询与连接
子查询指嵌套在查询内部,且必须始终出现在园括号内。
子查询可以包含多个关键字或条件
如DISTINCT、GROUP BY、ORDER BY、LIMIT、函数等。子查询的外层查询可以是:SELECT、INSERT、UPDATE、SET或DO。
子查询返回值
子查询可以返回标量、一行、一列、或子查询
使用比较运算符的子查询
使用比较运算符的子查询
= 、>、<、>=、<=、<>、!=、<=>
语法结构
operand comparison_operator subquery
SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price >= (SELECT ROUND(AVG(goods_price),2) FROM tdb_goods);
用ANY、SOME或ALL修饰的比较运算符
operand comparison_operator ANY(subquery)
operand comparison_operator SOME(subquery)
operand comparison_operator ALL(subquery)
SELECT goods_name,goods_price FROM tdb_goods WHERE goods_price > ANY (SELECT goods_price FROM tdb_goods WHERE goods_cate = '超级本');
使用[NOT] IN的子查询
语法结构
operand comparison_operator [NOT] IN (subquery)
=ANY 运算符与IN等效
!=ALL或<>ALL运算符与NOT IN等效。
SELECT goods_name,goods_price FROM tdb_goods WHERE goods_price IN (SELECT goods_price FROM tdb_goods WHERE goods_cate = '超级本');
使用[NOT] EXISTS的子查询
如果子查询返回任何行,EXISTS将返回true,否则为FALSE
分组
SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;
INSERT…SELECT
将查询结果写入到数据表
INSERT [INTO] tbl_name [(col_name,…)] SELECT …
查看表
DESC tdb_goods_cates;
将查询结果写到另一个表中
INSERT INTO tdb_goods_cates(cate_name) SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;
CREATE..SELECT
创建数据表同事将查询结果写入到数据表
CREATE TABLE [IF NOT EXISTS] tbl_name [(create_definition,…)] select_statement
CREATE TABLE tdb_goods_brands(brand_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,brand_name VARCHAR(40) NOT NULL)SELECT brand_name FROM tdb_goods GROUP BY brand_name;
连接类型
INNER JOIN,内连接
在MySQL中,JOIN,CROSS JOIN和INNER JOIN是等价的。
LEFT[OUTER] JSOIN,左外连接
RIGHT[OUTER] JOSIN,右外连接
语法结构
table_reference
{[INNER | CROSS] JOIN | {LEFT|RIGHT} [OUTER] JOIN} table_reference ON condition_expr
多表更新
UPDATE table_references SEL col_name1 = {expr1 | DEFAULT} [, col_name2={expr2|DEFAULT}]…[WHERE where_condition]
参照别的表修改本表
UPDATE tdb_goods INNER JOIN tdb_goods_cates ON goods_cate = cate_name SET goods_cate = cate_id;
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;
修改表结构
ALTER TABLE tdb_goods CHANGE goods_cate cate_id SMALLINT UNSIGNED NOT NULL, CHANGE brand_name brand_id SMALLINT UNSIGNED NOT NULL;
连接的语法结构
连接
MYSQL在SELECT语句、多表更新、多表删除语句中支持JOIN操作
数据表参照
table_reference
tbl_name[[AS] alias] | table_subquery [AS] alias 数据表可以使用tbl_name AS alias_name 或tbl_name alias_name 赋予别名。
tabe_subquery可以作为子查询使用在FROM子句中,这样的子查询必须为其赋予别名。
内连接
显示左表及右表符合连接条件的记录
外连接OUTER JOIN
左外连接
右外连接
外连接
自身连接
同一个数据表对其自身进行连接
-- 查找所有分类及其父类
SELECT s.type_id,s.type_name,p.type_name FROM tdb_goods_types AS s LEFT JOIN tdb_goods_types AS p ON s.parent_id = p.type_id;
-- 查找所有分类及其子类
SELECT p.type_id,p.type_name,s.type_name FROM tdb_goods_types AS p LEFT JOIN tdb_goods_types AS s ON s.parent_id = p.type_id;
多表删除
DELETE tbl_name[.*] [, tbl_name[.*]]...
FROM table_references
[WHERE where_condition]
-- 复制编号为12,20的两条记录
SELECT * FROM tdb_goods WHERE goods_id IN (19,20);
-- INSERT ... SELECT实现复制
INSERT tdb_goods(goods_name,cate_id,brand_id) SELECT goods_name,cate_id,brand_id FROM tdb_goods WHERE goods_id IN (19,20);
-- 查找重复记录
SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING count(goods_name) >= 2;
-- 删除重复记录
DELETE t1 FROM tdb_goods AS t1 LEFT JOIN (SELECT goods_id,goods_name FROM tdb_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;
MYSQL字符函数
mysql> SELECT CONCAT('test','test');
+-----------------------+
| CONCAT('test','test') |
+-----------------------+
| testtest |
+-----------------------+
mysql> SELECT CONCAT_WS('-','test','test');
+------------------------------+
| CONCAT_WS('-','test','test') |
+------------------------------+
| test-test |
+------------------------------+
mysql> SELECT FORMAT(1246.75,2);
+-------------------+
| FORMAT(1246.75,2) |
+-------------------+
| 1,246.75 |
+-------------------+
mysql> SELECT FORMAT(1246.75,1);
+-------------------+
| FORMAT(1246.75,1) |
+-------------------+
| 1,246.8 |
+-------------------+
mysql> SELECT LOWER('MYSQL');
+----------------+
| LOWER('MYSQL') |
+----------------+
| mysql |
+----------------+
mysql> SELECT LOWER(LEFT('MYSQL',2));
+------------------------+
| LOWER(LEFT('MYSQL',2)) |
+------------------------+
| my |
+------------------------+
mysql> SELECT LOWER(RIGHT('MYSQL',2));
+-------------------------+
| LOWER(RIGHT('MYSQL',2)) |
+-------------------------+
| ql |
+-------------------------+