MySQL的基本查询语句位SELECT语句,其语法规则如下
SELECT <字段列表>
[FROM <表名>] # 选择数据的来源
[WHERE <表达式>] # 按某个条件过滤查询到的数据
[GROUP BY <分组表达式>] # 按某个条件对数据分组
[HAVING <表达式>] # 对分组后的数据按某个条件进行过滤
[ORDER BY <字段名>] # 以某个变量为基准排序
[LIMIT <数量>]; # 限制打印的数据量
mysql> SELECT 3+5 AS sum; # 打印3+5,AS用于给前面的变量起变量名
+-----+
| sum |
+-----+
| 8 |
+-----+
1 row in set (0.00 sec)
mysql> SELECT *
FROM departments
LIMIT 10; # 打印数据表departments中的所有数据,限制输出10行
+---------+--------------------+
| dept_no | dept_name |
+---------+--------------------+
| d009 | Customer Service |
| d005 | Development |
| d002 | Finance |
| d003 | Human Resources |
| d001 | Marketing |
| d004 | Production |
| d006 | Quality Management |
| d008 | Research |
| d007 | Sales |
+---------+--------------------+
9 rows in set (0.00 sec)
mysql> SELECT dept_no AS a, dept_name AS b
FROM departments
LIMIT 10;
+------+--------------------+
| a | b |
+------+--------------------+
| d009 | Customer Service |
| d005 | Development |
| d002 | Finance |
| d003 | Human Resources |
| d001 | Marketing |
| d004 | Production |
| d006 | Quality Management |
| d008 | Research |
| d007 | Sales |
+------+--------------------+
9 rows in set (0.00 sec)
mysql> SELECT salary
FROM salaries
WHERE salary>60000 AND salary<70000 # 对查到的数据按某种条件过滤
LIMIT 10;
+--------+
| salary |
+--------+
| 60117 |
| 62102 |
| 66074 |
| 66596 |
| 66961 |
| 65828 |
| 65909 |
| 67534 |
| 69366 |
| 60770 |
+--------+
10 rows in set (0.00 sec)
IN
操作符常用来与关键字的查找BETWEEN AND
常用于范围查找AND
和OR
用于多条件查询,且AND
的优先级高于OR
IN
和OR
的功能相同,但是IN
的执行速度快于OR
mysql> SELECT salary
FROM salaries
ORDER BY salary # ORDER BY子语句默认按升序(ASC)排序
LIMIT 10;
+--------+
| salary |
+--------+
| 38623 |
| 38735 |
| 38786 |
| 38812 |
| 38836 |
| 38849 |
| 38850 |
| 38851 |
| 38859 |
| 38864 |
+--------+
10 rows in set (0.85 sec)
mysql> SELECT salary
FROM salaries
ORDER BY salary DESC # 指定按照降序排序
LIMIT 10;
+--------+
| salary |
+--------+
| 158220 |
| 157821 |
| 156286 |
| 155709 |
| 155513 |
| 155377 |
| 155190 |
| 154888 |
| 154885 |
| 154459 |
+--------+
10 rows in set (0.82 sec)
mysql> SELECT *
FROM salaries
ORDER BY salary DESC, emp_no # 可以指定多个排序的基准
LIMIT 10;
+--------+--------+------------+------------+
| emp_no | salary | from_date | to_date |
+--------+--------+------------+------------+
| 43624 | 158220 | 2002-03-22 | 9999-01-01 |
| 43624 | 157821 | 2001-03-22 | 2002-03-22 |
| 254466 | 156286 | 2001-08-04 | 9999-01-01 |
| 47978 | 155709 | 2002-07-14 | 9999-01-01 |
| 253939 | 155513 | 2002-04-11 | 9999-01-01 |
| 109334 | 155377 | 2000-02-12 | 2001-02-11 |
| 109334 | 155190 | 2002-02-11 | 9999-01-01 |
| 109334 | 154888 | 2001-02-11 | 2002-02-11 |
| 109334 | 154885 | 1999-02-12 | 2000-02-12 |
| 80823 | 154459 | 2002-02-22 | 9999-01-01 |
+--------+--------+------------+------------+
10 rows in set (0.95 sec)
MySQL可以使用通配符来进行匹配查找,关键字为LIKE
%:匹配任意长度的字符(包括长度为0的字符)
_:一次匹配一个字符
使用BINARY区分大小写
mysql> SELECT first_name
FROM employees
WHERE first_name LIKE BINARY "%A%" # 查询first_name中包含A的数据
# 去掉BINARY,就不区分大小写(如果建表时没有区分大写)
LIMIT 5;
+------------+
| first_name |
+------------+
| Anneke |
| Arif |
| Alain |
| Adamantios |
| Alejandro |
+------------+
5 rows in set (0.00 sec)
mysql> SELECT first_name
FROM employees
WHERE first_name LIKE BINARY "_a%" # 查询first_name中第二个字母为a的数据
LIMIT 5;
+------------+
| first_name |
+------------+
| Parto |
| Saniya |
| Mary |
| Patricio |
| Kazuhito |
+------------+
5 rows in set (0.00 sec)
mysql> SELECT emp_no
FROM salaries
ORDER BY emp_no
LIMIT 10;
+--------+
| emp_no |
+--------+
| 10001 |
| 10001 |
| 10001 |
| 10001 |
| 10001 |
| 10001 |
| 10001 |
| 10001 |
| 10001 |
| 10001 |
+--------+
10 rows in set (0.00 sec)
mysql> SELECT DISTINCT emp_no # 使用DISTINCT关键字对指定字段去重
FROM salaries
ORDER BY emp_no
LIMIT 10;
+--------+
| emp_no |
+--------+
| 10001 |
| 10002 |
| 10003 |
| 10004 |
| 10005 |
| 10006 |
| 10007 |
| 10008 |
| 10009 |
| 10010 |
+--------+
10 rows in set (0.00 sec)
DISTINCT
只能返回指定的去重的一列,不能返回其它列mysql> SELECT emp_no, COUNT(*) AS total # COUNT为函数,用于计算行的总数
FROM salaries
GROUP BY emp_no # 按照emp_no分组
ORDER BY total DESC
LIMIT 10;
+--------+-------+
| emp_no | total |
+--------+-------+
| 279953 | 18 |
| 239758 | 18 |
| 292257 | 18 |
| 235678 | 18 |
| 295842 | 18 |
| 470102 | 18 |
| 26608 | 18 |
| 16431 | 18 |
| 251839 | 18 |
| 105540 | 18 |
+--------+-------+
10 rows in set (0.68 sec)
上面的语句是查询按照emp_no分组,每一个emp_no的总数
mysql> SELECT id, COUNT(*) AS total
FROM groupTEST
GROUP BY id; # 按照id分组,所有id相同的分为一组
+------+-------+
| id | total |
+------+-------+
| 1 | 4 |
| 2 | 2 |
| 3 | 6 |
+------+-------+
3 rows in set (0.00 sec)
mysql> SELECT id, GROUP_CONCAT(name) # 将id相同的name通过分组拼接在一起
FROM groupTEST
GROUP BY id;
+------+--------------------------+
| id | GROUP_CONCAT(name) |
+------+--------------------------+
| 1 | Xiong,Wang,Li,Xiao |
| 2 | Xiong,Li |
| 3 | Li,Xiao,Wang,Xue,Xue,Xue |
+------+--------------------------+
3 rows in set (0.00 sec)
HAVING
过滤数据WHERE
是在分组前过滤数据,HAVING
是对分组后的数据进行过滤mysql> SELECT name, COUNT(*) AS total
FROM groupTEST
GROUP BY name;
+-------+-------+
| name | total |
+-------+-------+
| Li | 3 |
| Wang | 2 |
| Xiao | 2 |
| Xiong | 2 |
| Xue | 3 |
+-------+-------+
5 rows in set (0.00 sec)
mysql> SELECT name, COUNT(*) AS total
FROM groupTEST
GROUP BY name HAVING total<3;
+-------+-------+
| name | total |
+-------+-------+
| Wang | 2 |
| Xiao | 2 |
| Xiong | 2 |
+-------+-------+
3 rows in set (0.00 sec)
WITH ROLLUP
统计记录数量ROLLUP
和ORDER BY
互斥mysql> SELECT name, COUNT(*) AS total
FROM groupTEST
GROUP BY name WITH ROLLUP; # 使用该关键字后,会在表中最后一行显示统计的总数
+-------+-------+
| name | total |
+-------+-------+
| Li | 3 |
| Wang | 2 |
| Xiao | 2 |
| Xiong | 2 |
| Xue | 3 |
| NULL | 12 |
+-------+-------+
6 rows in set (0.00 sec)
mysql> SELECT id, name, COUNT(*) AS total
FROM groupTEST
GROUP BY id, name; # 先对第一个字段分组,再对第二个字段分组,COUNT计算的是最后一个分组
+------+-------+-------+
| id | name | total |
+------+-------+-------+
| 1 | Li | 1 |
| 1 | Wang | 1 |
| 1 | Xiao | 1 |
| 1 | Xiong | 1 |
| 2 | Li | 1 |
| 2 | Xiong | 1 |
| 3 | Li | 1 |
| 3 | Wang | 1 |
| 3 | Xiao | 1 |
| 3 | Xue | 3 |
+------+-------+-------+
10 rows in set (0.00 sec)
LIMIT
可以限制输出结果的位置与数量,基本语法如下
LIMIT [位置偏移量,] 行数
mysql> SELECT * FROM salaries
ORDER BY salary
LIMIT 10; # 输出结果前十行
+--------+--------+------------+------------+
| emp_no | salary | from_date | to_date |
+--------+--------+------------+------------+
| 253406 | 38623 | 2002-02-20 | 9999-01-01 |
| 49239 | 38735 | 1996-09-17 | 1997-09-17 |
| 281546 | 38786 | 1996-11-13 | 1997-06-26 |
| 15830 | 38812 | 2001-03-12 | 2002-03-12 |
| 64198 | 38836 | 1989-10-20 | 1990-10-20 |
| 475254 | 38849 | 1993-06-04 | 1994-06-04 |
| 50419 | 38850 | 1996-09-22 | 1997-09-22 |
| 34707 | 38851 | 1990-10-03 | 1991-10-03 |
| 49239 | 38859 | 1995-09-18 | 1996-09-17 |
| 274049 | 38864 | 1996-09-01 | 1997-09-01 |
+--------+--------+------------+------------+
10 rows in set (2.08 sec)
mysql> SELECT * FROM salaries
ORDER BY salary
LIMIT 2, 10; # 输出从第2行开始的10行
+--------+--------+------------+------------+
| emp_no | salary | from_date | to_date |
+--------+--------+------------+------------+
| 281546 | 38786 | 1996-11-13 | 1997-06-26 |
| 15830 | 38812 | 2001-03-12 | 2002-03-12 |
| 64198 | 38836 | 1989-10-20 | 1990-10-20 |
| 475254 | 38849 | 1993-06-04 | 1994-06-04 |
| 50419 | 38850 | 1996-09-22 | 1997-09-22 |
| 34707 | 38851 | 1990-10-03 | 1991-10-03 |
| 49239 | 38859 | 1995-09-18 | 1996-09-17 |
| 274049 | 38864 | 1996-09-01 | 1997-09-01 |
| 473390 | 38872 | 1995-03-20 | 1995-09-22 |
| 12444 | 38874 | 1990-08-15 | 1991-08-15 |
+--------+--------+------------+------------+
10 rows in set (0.96 sec)
LIMIT 3, 4
和LIMIT 3 OFFSET 4
效果一样MySQL提供一些查询功能,可以对获取的数据进行分析和报告
COUNT()返回结果中指定列的行数
mysql> SELECT COUNT(*)
FROM salaries
WHERE salary BETWEEN 50000 AND 70000;
+----------+
| COUNT(*) |
+----------+
| 1258826 |
+----------+
1 row in set (0.61 sec)
mysql> SELECT emp_no, salary
FROM salaries
WHERE emp_no BETWEEN 10000 AND 10001;
+--------+--------+
| emp_no | salary |
+--------+--------+
| 10001 | 60117 |
| 10001 | 62102 |
| 10001 | 66074 |
| 10001 | 66596 |
| 10001 | 66961 |
| 10001 | 71046 |
| 10001 | 74333 |
| 10001 | 75286 |
| 10001 | 75994 |
| 10001 | 76884 |
| 10001 | 80013 |
| 10001 | 81025 |
| 10001 | 81097 |
| 10001 | 84917 |
| 10001 | 85112 |
| 10001 | 85097 |
| 10001 | 88958 |
+--------+--------+
17 rows in set (0.00 sec)
mysql> SELECT SUM(salary)
FROM salaries
WHERE emp_no BETWEEN 10000 AND 10001;
+-------------+
| SUM(salary) |
+-------------+
| 1281612 |
+-------------+
1 row in set (0.00 sec)
mysql> SELECT emp_no, AVG(salary) AS avg_salary
FROM salaries
GROUP BY emp_no
ORDER BY avg_salary
LIMIT 10;
+--------+------------+
| emp_no | avg_salary |
+--------+------------+
| 15830 | 39299.5000 |
| 253406 | 39332.7500 |
| 473390 | 39372.5000 |
| 281546 | 39417.2500 |
| 466226 | 39442.7500 |
| 401786 | 39453.7500 |
| 245832 | 39515.2500 |
| 230890 | 39520.0000 |
| 496197 | 39573.3333 |
| 496848 | 39584.0000 |
+--------+------------+
10 rows in set (1.08 sec)
MAX
和MIN
可以查找数字和日期mysql> SELECT MAX(salary)
FROM salaries;
+-------------+
| MAX(salary) |
+-------------+
| 158220 |
+-------------+
1 row in set (0.60 sec)
mysql> SELECT MIN(salary)
FROM salaries;
+-------------+
| MIN(salary) |
+-------------+
| 38623 |
+-------------+
1 row in set (0.59 sec)
mysql> SELECT MIN(birth_date)
FROM employees;
+-----------------+
| MIN(birth_date) |
+-----------------+
| 1952-02-01 |
+-----------------+
1 row in set (0.18 sec)
mysql> SELECT MAX(birth_date)
FROM employees;
+-----------------+
| MAX(birth_date) |
+-----------------+
| 1965-02-01 |
+-----------------+
1 row in set (0.08 sec)
创建数据库emp(员工表)、dept(部门表)、salaries(薪水表)
mysql> CREATE TABLE emp (
emp_no INT AUTO_INCREMENT KEY,
name VARCHAR(20) NOT NULL,
dept_no INT,
salary INT);
Query OK, 0 rows affected (0.04 sec)
mysql> CREATE TABLE dept (
dept_no INT KEY,
dname VARCHAR(20));
Query OK, 0 rows affected (0.04 sec)
mysql> CREATE TABLE salaries (
low INT,
high INT,
level INT);
Query OK, 0 rows affected (0.04 sec)
mysql> INSERT INTO emp VALUES
-> (1, "Xiong", 101, 1000),
-> (2, "Wang", 101, 1200),
-> (3, "Li", 103, 1500),
-> (4, "Zhao", 104, 2300),
-> (5, "Xiao", 104, 2000),
-> (6, "Hu", 105, 2100);
Query OK, 6 rows affected (0.02 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> INSERT INTO dept VALUES
-> (101, "部门1"),
-> (102, "部门2"),
-> (103, "部门3"),
-> (104, "部门4"),
-> (105, "部门5");
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> INSERT INTO salaries VALUES
-> (1000, 1500, 1),
-> (1501, 2000, 2),
-> (2001, 2500, 3),
-> (2501, 3000, 4),
-> (3001, 3055, 5);
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
等值连接
mysql> SELECT e.emp_no, e.name, d.dept_no, d.dname
FROM emp e # AS可省略
INNER JOIN dept d
ON e.dept_no=d.dept_no;
+--------+-------+---------+---------+
| emp_no | name | dept_no | dname |
+--------+-------+---------+---------+
| 1 | Xiong | 101 | 部门1 |
| 2 | Wang | 101 | 部门1 |
| 3 | Li | 103 | 部门3 |
| 4 | Zhao | 104 | 部门4 |
| 5 | Xiao | 104 | 部门4 |
| 6 | Hu | 105 | 部门5 |
+--------+-------+---------+---------+
6 rows in set (0.00 sec)
非等值连接
mysql> SELECT e.name, e.salary, s.level
FROM emp e
INNER JOIN salaries s
ON e.salary BETWEEN s.low AND s.high;
+-------+--------+-------+
| name | salary | level |
+-------+--------+-------+
| Xiong | 1000 | 1 |
| Wang | 1200 | 1 |
| Li | 1500 | 1 |
| Zhao | 2300 | 3 |
| Xiao | 2000 | 2 |
| Hu | 2100 | 3 |
+-------+--------+-------+
6 rows in set (0.01 sec)
mysql> SELECT e.name, e.salary, s.level # 右外连接
FROM emp e
RIGHT OUTER JOIN salaries s
ON e.salary BETWEEN s.low AND s.high;
+-------+--------+-------+
| name | salary | level |
+-------+--------+-------+
| Xiong | 1000 | 1 |
| Wang | 1200 | 1 |
| Li | 1500 | 1 |
| Zhao | 2300 | 3 |
| Xiao | 2000 | 2 |
| Hu | 2100 | 3 |
| NULL | NULL | 4 |
| NULL | NULL | 5 |
+-------+--------+-------+
8 rows in set (0.00 sec)
mysql> SELECT e.name, e.salary, s.level # 左外连接
FROM salaries s
LEFT OUTER JOIN emp e
ON e.salary BETWEEN s.low AND s.high;
+-------+--------+-------+
| name | salary | level |
+-------+--------+-------+
| Xiong | 1000 | 1 |
| Wang | 1200 | 1 |
| Li | 1500 | 1 |
| Zhao | 2300 | 3 |
| Xiao | 2000 | 2 |
| Hu | 2100 | 3 |
| NULL | NULL | 4 |
| NULL | NULL | 5 |
+-------+--------+-------+
8 rows in set (0.00 sec)
mysql> SELECT e.name, e.salary, s.level
FROM emp e
INNER JOIN salaries s
ON e.salary BETWEEN s.low AND s.high AND s.level<=2;
+-------+--------+-------+
| name | salary | level |
+-------+--------+-------+
| Xiong | 1000 | 1 |
| Wang | 1200 | 1 |
| Li | 1500 | 1 |
| Xiao | 2000 | 2 |
+-------+--------+-------+
4 rows in set (0.00 sec)
创建两张表为示例
mysql> CREATE TABLE t1 (n1 INT);
Query OK, 0 rows affected (0.14 sec)
mysql> CREATE TABLE t2 (n2 INT);
Query OK, 0 rows affected (0.08 sec)
mysql> INSERT INTO t1 VALUES (10), (15), (20), (25);
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> INSERT INTO t2 VALUES (11), (12), (21), (23);
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> SELECT n1 FROM t1
-> WHERE n1>ANY
-> (SELECT n2 FROM t2);
+------+
| n1 |
+------+
| 15 |
| 20 |
| 25 |
+------+
3 rows in set (0.00 sec)
# n2为11、12、21、22,n1为10、15、20、25,只有10不满足条件
# 15、20、25均大于11、12、21、22中的任意一个
mysql> SELECT n1 FROM t1
-> WHERE n1>ALL
-> (SELECT n2 FROM t2);
+------+
| n1 |
+------+
| 25 |
+------+
1 row in set (0.00 sec)
# n2为11、12、21、22,n1为10、15、20、25,只有25满足条件
# 只有25满足大于11、12、21、22的所有条件
下例中,t2的n2均小于30
mysql> SELECT * FROM t1
-> WHERE EXISTS
-> (SELECT n2 FROM t2 WHERE n2 > 30);
Empty set (0.00 sec)
mysql> SELECT * FROM t1
-> WHERE NOT EXISTS
-> (SELECT n2 FROM t2 WHERE n2 > 30);
+------+
| n1 |
+------+
| 10 |
| 15 |
| 20 |
| 25 |
+------+
4 rows in set (0.00 sec)
mysql> SELECT n1 FROM t1
-> WHERE n1 IN
-> (SELECT n2 FROM t2 WHERE n2<15);
Empty set (0.00 sec)
mysql> CREATE TABLE fruits (id INT, price INT, name VARCHAR(10));
Query OK, 0 rows affected (0.11 sec)
mysql> INSERT INTO fruits VALUES
-> (1, 100, "苹果"),
-> (2, 102, "梨子"),
-> (3, 120, "桃子"),
-> (4, 121, "草莓");
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
# 查询fruits表中price在100~110之间及id为4的水果的信息
mysql> SELECT id, name
-> FROM fruits
-> WHERE fruits.price BETWEEN 100 AND 110
-> UNION ALL
-> SELECT id, name
-> FROM fruits
-> WHERE fruits.id=4;
+------+--------+
| id | name |
+------+--------+
| 1 | 苹果 |
| 2 | 梨子 |
| 4 | 草莓 |
+------+--------+
3 rows in set (0.00 sec)
基本语法如下
INSERT INTO <表名> [字段名] VALUES <列值 [, ...]>;
mysql> CREATE TABLE t1
(a INT, b INT);
Query OK, 0 rows affected (0.06 sec)
mysql> CREATE TABLE t2
(a INT, b INT, c INT);
Query OK, 0 rows affected (0.04 sec)
mysql> INSERT INTO t1 VALUES
-> (100, 200),
-> (101, 300),
-> (102, 400);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
# Records为插入的条数纪录
# Duplicates为被忽略的纪录
# Warnings表明有问题的数据值
mysql> INSERT INTO t2 VALUES
-> (103, 1, 500),
-> (104, 2, 600),
-> (105, 3, 700);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> INSERT INTO t2 (a, c)
-> VALUES
-> (1, 100),
-> (2, 200);
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t2; # 对未插入值的列默认为NULL
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | NULL | 100 |
| 2 | NULL | 200 |
+------+------+------+
2 rows in set (0.00 sec)
mysql> INSERT INTO t1 (a, b)
-> SELECT a, c FROM t2;
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t1;
+------+------+
| a | b |
+------+------+
| 1 | 100 |
| 2 | 200 |
+------+------+
2 rows in set (0.00 sec)
mysql> INSERT INTO t2
-> SELECT * FROM t2;
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t2;
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | NULL | 100 |
| 2 | NULL | 200 |
| 1 | NULL | 100 |
| 2 | NULL | 200 |
+------+------+------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM fruits;
+------+-------+--------+
| id | price | name |
+------+-------+--------+
| 1 | 100 | 苹果 |
| 2 | 102 | 梨子 |
| 3 | 120 | 桃子 |
| 4 | 121 | 草莓 |
+------+-------+--------+
4 rows in set (0.00 sec)
mysql> UPDATE fruits
SET id=1,price=1000,name="车厘子"
WHERE id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM fruits;
+------+-------+-----------+
| id | price | name |
+------+-------+-----------+
| 1 | 1000 | 车厘子 |
| 2 | 102 | 梨子 |
| 3 | 120 | 桃子 |
| 4 | 121 | 草莓 |
+------+-------+-----------+
4 rows in set (0.00 sec)
mysql> UPDATE fruits
SET id=1,price=1000,name="车厘子";
Query OK, 3 rows affected (0.01 sec)
Rows matched: 4 Changed: 3 Warnings: 0
mysql> SELECT * FROM fruits;
+------+-------+-----------+
| id | price | name |
+------+-------+-----------+
| 1 | 1000 | 车厘子 |
| 1 | 1000 | 车厘子 |
| 1 | 1000 | 车厘子 |
| 1 | 1000 | 车厘子 |
+------+-------+-----------+
4 rows in set (0.00 sec)
基本语法如下
DELETE FROM <表名> [WHERE <条件>];
mysql> SELECT * FROM fruits;
+------+-------+-----------+
| id | price | name |
+------+-------+-----------+
| 1 | 1000 | 车厘子 |
| 1 | 1000 | 车厘子 |
| 1 | 1000 | 车厘子 |
| 1 | 1000 | 车厘子 |
+------+-------+-----------+
4 rows in set (0.00 sec)
mysql> DELETE FROM fruits
WHERE id=1;
Query OK, 4 rows affected (0.02 sec)
mysql> SELECT * FROM fruits;
Empty set (0.00 sec)
或者可以使用TRUNCATE TABLE语句,该语句是直接删除原表,再创建一张新表,所以效率更高
mysql> SELECT * FROM t1;
+------+
| n1 |
+------+
| 10 |
| 15 |
| 20 |
| 25 |
+------+
4 rows in set (0.00 sec)
mysql> TRUNCATE TABLE t1;
Query OK, 0 rows affected (0.03 sec)
mysql> SELECT * FROM t1;
Empty set (0.00 sec)
计算列:即某列的值是根据其它列计算而来的
CREATE TABLE和ALTER TABLE语句均支持计算列
使用GENERATED ALWAYS关键字表示计算列
基本语法如下
CREATE TABLE <表名> (
<字段名 字段类型> GENERATED ALWAYS AS (表达式) [VIRTUAL|STORED] [约束条件]
);
例子如下
mysql> CREATE TABLE tab1 (
a INT,
b INT,
c INT GENERATED ALWAYS AS (a*b) VIRTUAL);
Query OK, 0 rows affected (0.06 sec)
mysql> INSERT INTO tab1 (a, b)
VALUES
(2, 3);
Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM tab1;
+------+------+------+
| a | b | c |
+------+------+------+
| 2 | 3 | 6 |
+------+------+------+
1 row in set (0.00 sec)