学习目标:
delete删除数据记录与like子句指定条件、union操作符
学习内容:
1.delete删除数据
delete from <表名称> ; 删除表中所有数据
如果没有指定 WHERE 子句,MySQL 表中的所有记录将被删除。
delete from <表名称> where age=21; 删除表中指定数据
可以在 WHERE 子句中指定任何条件
mysql> select * from polon;
+----+-----------+-----+-----+-----------------+
| id | name | sex | age | submission_data |
+----+-----------+-----+-----+-----------------+
| 2 | 李四 | 男 | 21 | 2021-01-30 |
| 3 | 王五 | 女 | 19 | 2016-08-09 |
| 4 | 张三 | 男 | 18 | 2018-07-03 |
| 5 | 二麻子 | 男 | 18 | 2018-07-03 |
+----+-----------+-----+-----+-----------------+
4 rows in set (0.00 sec)
mysql> delete from polon where age=21; #删除age为21的指定数据
Query OK, 1 row affected (0.50 sec)
mysql> select * from polon;
+----+-----------+-----+-----+-----------------+
| id | name | sex | age | submission_data |
+----+-----------+-----+-----+-----------------+
| 3 | 王五 | 女 | 19 | 2016-08-09 |
| 4 | 张三 | 男 | 18 | 2018-07-03 |
| 5 | 二麻子 | 男 | 18 | 2018-07-03 |
+----+-----------+-----+-----+-----------------+
3 rows in set (0.00 sec)
2.like子句
select * from <表名称> where <指定条件> like ‘%<指定条件>’; 以指定条件查询,得出所有记录
mysql> select * from polon where age like '%8'; #查询age中以8结尾的所有记录
+----+-----------+-----+-----+-----------------+
| id | name | sex | age | submission_data |
+----+-----------+-----+-----+-----------------+
| 4 | 张三 | 男 | 18 | 2018-07-03 |
| 5 | 二麻子 | 男 | 18 | 2018-07-03 |
+----+-----------+-----+-----+-----------------+
2 rows in set (0.01 sec)
可以在 WHERE 子句中使用LIKE子句。
可以使用LIKE子句代替等号 =。
LIKE 通常与 % 一同使用,类似于一个元字符的搜索。
可以使用 AND 或者 OR 指定一个或多个条件。
可以在 DELETE 或 UPDATE 命令中使用 WHERE…LIKE 子句来指定条件。
3.union操作符
UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。
select <列名称> from <表名称1>
union
select <列名称> from <表名称2>
order by <列名称>;
mysql> show tables; #获取数据表
+------------------+
| Tables_in_runoob |
+------------------+
| apps |
| runoob_tb1 |
+------------------+
2 rows in set (0.00 sec)
mysql> select * from apps; #获取apps表内容
+----+--------+---------------+------------+
| id | name | runoob_author | data |
+----+--------+---------------+------------+
| 3 | QQ | 马化腾 | 2019-07-05 |
| 5 | 微信 | 王江 | 2013-04-01 |
+----+--------+---------------+------------+
2 rows in set (0.00 sec)
mysql> select * from runoob_tb1; #获取runoob_tb1表内容
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_data |
+-----------+--------------+---------------+-----------------+
| 1 | jave教程 | runoob.com | 2016-04-03 |
| 2 | 我爱中华 | 刘桑 | 2021-01-29 |
| 3 | 琰潇秋 | 李旺 | 2014-05-07 |
| 4 | 学习php | 王江 | 2015-08-05 |
| 5 | python | 李桑 | 2021-01-30 |
+-----------+--------------+---------------+-----------------+
5 rows in set (0.00 sec)
mysql> select runoob_author from apps
-> union #相当于两张数据表中的runoob_author列 去重后的并集
-> select runoob_author from runoob_tb1
-> order by runoob_author; #使用order命令
+---------------+
| runoob_author |
+---------------+
| runoob.com |
| 刘桑 |
| 李旺 |
| 李桑 |
| 王江 |
| 马化腾 |
+---------------+
6 rows in set (0.47 sec)
或者使用union all获取所有的数据(未去重)
mysql> select runoob_author from apps
-> union all #获取两张表中runoob_author 列中所有的数据,未去重
-> select runoob_author from runoob_tb1
-> order by runoob_author;
+---------------+
| runoob_author |
+---------------+
| runoob.com |
| 刘桑 |
| 李旺 |
| 李桑 |
| 王江 | #相同
| 王江 | #相同
| 马化腾 |
+---------------+
7 rows in set (0.00 sec)
也可以使用where子句
mysql> select runoob_author,name from apps #两者之间使用 , 隔开
-> where runoob_author="王江" # where指定条件使runoob_author=name,前者等于后者
-> union all
-> select runoob_author,runoob_title from runoob_tb1
-> where runoob_author="王江" # where指定条件使runoob_author=runoob_title,前者等于后者
-> order by runoob_author;
+---------------+-----------+
| runoob_author | name |
+---------------+-----------+
| 王江 | 微信 |
| 王江 | 学习php |
+---------------+-----------+
2 rows in set (0.00 sec)
UNION 语句:用于将不同表中相同列中查询的数据展示出来;(不包括重复数据)
UNION ALL 语句:用于将不同表中相同列中查询的数据展示出来;(包括重复数据)