SELECT 字段1 FROM 表1 WHERE EXISTS (SELECT * FROM 表2 WHERE 条件);
例:
use train_ticket;
select money from class01 where exists (select * from class01 where money = '80');
select money from class01 where exists (select * from class01 where money = '87');
SELECT 字段 FROM 表1 INNER JOIN 表2 ON 表1.字段 = 表2.字段;
例:
select * from class01 as a inner join class02 as b on a.name = b.name;
SELECT 字段 FROM 表1 LEFT JOIN 表2 ON 表1.字段 = 表2.字段;
例:
select * from calss01 a left join class02 b on a.name =b.name;
SELECT 字段 FROM 表1 RIGHT JOIN 表2 ON 表1.字段 = 表2.字段;
例:
select * from class01 a right join class02 b on a.name=b.name;
CREATE VIEW 视图表名 AS SELECT语句;
例:
select a.id,a.name,sum(b.money) from class01 a inner join class02 b on a.name=b.name GROUP BY id;
create view st as select a.id,a.name,sum(b.money) from class01 a inner join class02 b on a.name =b.name group by id;
show tables;
select * from ST;
drop view ST;
#删除视图表
SELECT语句1 UNION SELECT语句2;
或
SELECT语句1 UNION ALL SELECT语句2;
#UNION ALL:将生成结果的资料值都列出来,无论有无重复
例:
select name from class01 union select money from class02;
select name from class01 union select name from class02;
select name from class01 union all select name from class02;
select * from FARE;
select * from class01;
select adress from (select a.address from class a inner join class02 b on a.name = b.name) c group by c.address;
select name,address from class01 where name in (select name from class02);
select distinct name from class01 where name not in (select name from class02);
select a.*,b.* from class01 a left join class02 b using(name);
select distinct site from FARE A left join REGION B using(site) where B.site is null;
7.CASE
SELECT CASE (字段名)
WHEN 条件1 THEN 结果1
WHEN 条件2 THEN 结果2
……
ELSE 结果N
END
FROM 表名
#条件可以是一个数值或是公式,且 ELSE 子句不是必须的
例:
select * from FARE;
mysql> select case site
-> when 'najing' then money + 500
-> when 'tianjin' then money - 500
-> else money * 2
-> end
-> A,site
-> from FARE;
DELIMITER $$ #将语句的结束符号从分号;临时修改,以防出问题,可以自定义
CREATE PROCEDURE XXX() #创建存储过程,过程名自定义,()可带参数
BEGIN #过程体以关键字BEGIN开始
select * from xxx; #过程体语句
END$$ #过程体以关键字END结尾
DELIMITER ; #将语句的结束符号恢复为分号
例:
mysql> delimiter $$
mysql> create procedure XCF()
-> begin
-> select * from FARE;
-> end $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
CALL XCF;
show create procedure [数据库.]储存过程名; #查看某个储存过程的具体信息
show create procedure XXX;
show procedure status [like '%XXX%'] \G
例:
show create procedure train_ticket.XCF\G
show procedure status like '%XCF%'\G
mysql> delimiter $$
mysql> create procedure XCF0(IN place char(20))
-> begin
-> select * from FARE where site=place;
-> end $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter $$
mysql> call XCF0('nanjing');
-> $$
+---------+-------+------------+
| site | money | date |
+---------+-------+------------+
| nanjing | 2000 | 2021-02-07 |
+---------+-------+------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
DROP PROCEDURE IF EXISTS Proc;
#仅当存在时删除,如果指定的过程不存在,则会产生一个错误
例:
drop procedure if exists XCF0;
mysql> DELIMITER $$
mysql> CREATE PROCEDURE XCF1(IN num int(10))
-> BEGIN
-> declare var int;
-> set var=num*2;
-> if var>=10 then
-> update FARE set money=money+1;
-> else
-> update FARE set money=money-1;
-> end if;
-> END $$
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
CALL XCF1(5);
CALL XCF1(4);
#调用过程体
select money from FARE;
mysql> create table xcf(id int(5));
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER $$
mysql> CREATE PROCEDURE XCF2()
-> BEGIN
-> declare var int;
-> set var=0;
-> while var<9 do
-> insert into xcf values(var);
-> set var=var+1;
-> end while;
-> END $$
Query OK, 0 rows affected (0.00 sec)
mysql> CALL XCF2;
-> $$
Query OK, 1 row affected (0.01 sec)
mysql> select * from xcf;
-> $$
+------+
| id |
+------+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
+------+
9 rows in set (0.00 sec)