MySQL数据库多表查询
程序员文章站
2022-11-06 22:47:55
MySQL数据库多表查询 [toc] 多表查询 1. 查询结果来自于多张表,即多表查询 子查询 常用在WHERE子句中的子查询 1. 用于比较表达式中的子查询;子查询仅能返回单个值(查询s1表中大于平均年龄的人) 2. 查询结果嵌入到另一个表里,小数转换整数会四舍五入 3. 多表查询: 用子循环查看 ......
目录
mysql数据库多表查询
多表查询
- 查询结果来自于多张表,即多表查询
子查询:在sql语句嵌套着查询语句,性能较差,基于某语句的查询结果再次进行的查询 联合查询:union 交叉连接:笛卡尔乘积 内连接: 等值连接:让表之间的字段以“等值”建立连接关系 不等值连接:不等值连接查询就是无条件判断,若查询多个表内的数据,其中的数据不会同步,各自把各自的展现出来,没有任何关联。 自然连接:去掉重复列的等值连接 外连接: 左外连接:from tb1 left join tb2 on tb1.col=tb2.col 右外连接:from tb1 right join tb2 on tb1.col=tb2.col 自连接:本表和本表进行连接查询
子查询
常用在where子句中的子查询
- 用于比较表达式中的子查询;子查询仅能返回单个值(查询s1表中大于平均年龄的人)
mariadb [hellodb]> select * from s1 where age > (select avg(age) from s1); +-------+--------------+-------+-----+--------+---------+-----------+ | stuid | name | phone | age | gender | classid | teacherid | +-------+--------------+-------+-----+--------+---------+-----------+ | 3 | xie yanke | null | 53 | m | 2 | 16 | | 4 | ding dian | null | 32 | m | 4 | 4 | | 5 | yu yutong | null | 26 | m | 3 | 1 | | 6 | shi qing | null | 46 | m | 5 | null | | 13 | tian boguang | null | 33 | m | 2 | null | | 24 | xu xian | null | 27 | m | null | null | | 25 | sun dasheng | null | 100 | m | null | null | +-------+--------------+-------+-----+--------+---------+-----------+ 7 rows in set (0.01 sec)
- 查询结果嵌入到另一个表里,小数转换整数会四舍五入
mariadb [hellodb]> select avg(age) from s1 ; (查看s1表平均年龄) +----------+ | avg(age) | +----------+ | 25.0857 | +----------+ 1 row in set (0.00 sec) mariadb [hellodb]> select * from teachers; (原来的表内容) +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | | 4 | lin chaoying | 26 | f | +-----+---------------+-----+--------+ 4 rows in set (0.00 sec) mariadb [hellodb]> update teachers set age=(select avg(age) from s1); (插入查询结果的表内容,没有指定字段会改掉所有) query ok, 4 rows affected (0.00 sec) rows matched: 4 changed: 4 warnings: 0 mariadb [hellodb]> select * from teachers; +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 25 | m | | 2 | zhang sanfeng | 25 | m | | 3 | miejue shitai | 25 | f | | 4 | lin chaoying | 25 | f | +-----+---------------+-----+--------+ 4 rows in set (0.00 sec) mariadb [hellodb]> update teachers set age=48 where tid=4; (把tid为4的age修改为48做下面实验用) query ok, 1 row affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0 mariadb [hellodb]> select * from teachers; +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 25 | m | | 2 | zhang sanfeng | 25 | m | | 3 | miejue shitai | 25 | f | | 4 | lin chaoying | 48 | f | +-----+---------------+-----+--------+ 4 rows in set (0.00 sec) mariadb [hellodb]> update teachers set age=(select avg(age) from s1) where tid=4; (指定tid为4的age字段修改) query ok, 1 row affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0 mariadb [hellodb]> select * from teachers; +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 25 | m | | 2 | zhang sanfeng | 25 | m | | 3 | miejue shitai | 25 | f | | 4 | lin chaoying | 25 | f | +-----+---------------+-----+--------+ 4 rows in set (0.00 sec)
- 多表查询:
用子循环查看s1表,显示teachers表年龄大于s1表平均年龄的人的信息。
mariadb [hellodb]> update teachers set age=45 where tid=1; query ok, 1 row affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0 mariadb [hellodb]> update teachers set age=94 where tid=2; query ok, 1 row affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0 mariadb [hellodb]> update teachers set age=77 where tid=3; query ok, 1 row affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0 mariadb [hellodb]> select * from teachers; +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | | 4 | lin chaoying | 25 | f | +-----+---------------+-----+--------+ 4 rows in set (0.00 sec) (以上是把年龄修改回来做实验) mariadb [hellodb]> select * from teachers where age > (select avg(age) from s1); (多表子循环查询平均年龄大于25的人) +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | +-----+---------------+-----+--------+ 3 rows in set (0.00 sec) mariadb [hellodb]> update teachers set age=26 where tid=4; (修改一下最后一条的年龄为26) query ok, 1 row affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0 mariadb [hellodb]> select * from teachers where age > (select avg(age) from s1); (最后一条也大于25就显示出来了) +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | | 4 | lin chaoying | 26 | f | +-----+---------------+-----+--------+ 4 rows in set (0.00 sec)
联合查询
- union 纵向合并两张表,表头来自第一条查询记录.
mariadb [hellodb]> select * from teachers -> union -> select stuid,name,age,gender from s1; +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | | 4 | lin chaoying | 26 | f | | 1 | shi zhongyu | 22 | m | | 2 | shi potian | 22 | m | | 3 | xie yanke | 53 | m | | 4 | ding dian | 32 | m | | 5 | yu yutong | 26 | m | | 6 | shi qing | 46 | m | | 7 | xi ren | 19 | f | | 8 | lin daiyu | 17 | f | | 9 | ren yingying | 20 | f | | 10 | yue lingshan | 19 | f | | 11 | yuan chengzhi | 23 | m | | 12 | wen qingqing | 19 | f | | 13 | tian boguang | 33 | m | | 14 | lu wushuang | 17 | f | | 15 | duan yu | 19 | m | | 16 | xu zhu | 21 | m | | 17 | lin chong | 25 | m | | 18 | hua rong | 23 | m | | 19 | xue baochai | 18 | f | | 20 | diao chan | 19 | f | | 21 | huang yueying | 22 | f | | 22 | xiao qiao | 20 | f | | 23 | ma chao | 23 | m | | 24 | xu xian | 27 | m | | 25 | sun dasheng | 100 | m | | 26 | xietingfeng | 23 | m | | 27 | liudehua | 18 | f | | 28 | mahuateng | 20 | m | | 29 | wuyanzu | 19 | m | | 30 | wuzetian | 21 | f | | 31 | song jiang | 18 | m | | 32 | zhang sanfeng | 18 | m | | 33 | miejue shitai | 18 | f | | 34 | lin chaoying | 18 | f | | 38 | abc | 20 | m | +-----+---------------+-----+--------+ 39 rows in set (0.00 sec) mariadb [hellodb]> select tid as id ,name,age,gender from teachers union select stuid,name,age,gender from s1; (起个别名替换掉表头的tid并纵向合并两张表) +----+---------------+-----+--------+ | id | name | age | gender | +----+---------------+-----+--------+ | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | | 4 | lin chaoying | 26 | f | | 1 | shi zhongyu | 22 | m | | 2 | shi potian | 22 | m | | 3 | xie yanke | 53 | m | | 4 | ding dian | 32 | m | | 5 | yu yutong | 26 | m | | 6 | shi qing | 46 | m | | 7 | xi ren | 19 | f | | 8 | lin daiyu | 17 | f | | 9 | ren yingying | 20 | f | | 10 | yue lingshan | 19 | f | | 11 | yuan chengzhi | 23 | m | | 12 | wen qingqing | 19 | f | | 13 | tian boguang | 33 | m | | 14 | lu wushuang | 17 | f | | 15 | duan yu | 19 | m | | 16 | xu zhu | 21 | m | | 17 | lin chong | 25 | m | | 18 | hua rong | 23 | m | | 19 | xue baochai | 18 | f | | 20 | diao chan | 19 | f | | 21 | huang yueying | 22 | f | | 22 | xiao qiao | 20 | f | | 23 | ma chao | 23 | m | | 24 | xu xian | 27 | m | | 25 | sun dasheng | 100 | m | | 26 | xietingfeng | 23 | m | | 27 | liudehua | 18 | f | | 28 | mahuateng | 20 | m | | 29 | wuyanzu | 19 | m | | 30 | wuzetian | 21 | f | | 31 | song jiang | 18 | m | | 32 | zhang sanfeng | 18 | m | | 33 | miejue shitai | 18 | f | | 34 | lin chaoying | 18 | f | | 38 | abc | 20 | m | +----+---------------+-----+--------+ 39 rows in set (0.00 sec)
- union 自己和自己相连可以去重。
mariadb [hellodb]> create table t2 select * from teachers; (先导一张表出来做实验不能有主键,所以只用了这种方法导了数据没有把主键导过来) query ok, 4 rows affected (0.01 sec) records: 4 duplicates: 0 warnings: 0 mariadb [hellodb]> select * from t2; +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | | 4 | lin chaoying | 26 | f | +-----+---------------+-----+--------+ 4 rows in set (0.00 sec) mariadb [hellodb]> desc t2; +--------+----------------------+------+-----+---------+-------+ | field | type | null | key | default | extra | +--------+----------------------+------+-----+---------+-------+ | tid | smallint(5) unsigned | no | | 0 | | | name | varchar(100) | no | | null | | | age | tinyint(3) unsigned | no | | null | | | gender | enum('f','m') | yes | | null | | +--------+----------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) (添加重复的行做实验) mariadb [hellodb]> insert into t2 set tid=4,name='linchaoying',age=26,gender='f'; (这条记录添加的只有name不一样少了个空格) query ok, 1 row affected (0.00 sec) mariadb [hellodb]> insert into t2 set tid=4,name='lin chaoying',age=26,gender='f'; (完全一样加了一行) query ok, 1 row affected (0.00 sec) mariadb [hellodb]> select * from t2; +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | | 4 | lin chaoying | 26 | f | | 4 | linchaoying | 26 | f | | 4 | lin chaoying | 26 | f | +-----+---------------+-----+--------+ 6 rows in set (0.00 sec) mariadb [hellodb]> select * from t2 union select * from t2; (用union过滤掉重复的行,少一个空格的那条记录过滤不了) +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | | 4 | lin chaoying | 26 | f | | 4 | linchaoying | 26 | f | +-----+---------------+-----+--------+ 5 rows in set (0.00 sec)
- 使用all 简单连接两张表不去重
mariadb [hellodb]> select * from t2 union all select * from t2; +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | | 4 | lin chaoying | 26 | f | | 4 | linchaoying | 26 | f | | 4 | lin chaoying | 26 | f | | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | | 4 | lin chaoying | 26 | f | | 4 | linchaoying | 26 | f | | 4 | lin chaoying | 26 | f | +-----+---------------+-----+--------+ 12 rows in set (0.00 sec)
交叉连接
两张表横向组合,类似于笛卡尔乘积。 (cross join)
- 两张表使用交叉连接就是这张表的每一行去和另一张表的所有行组合一遍,形成新的行。
mariadb [hellodb]> select * from s1 cross join teachers; +-------+---------------+-------+-----+--------+---------+-----------+-----+---------------+-----+--------+ | stuid | name | phone | age | gender | classid | teacherid | tid | name | age | gender | +-------+---------------+-------+-----+--------+---------+-----------+-----+---------------+-----+--------+ | 1 | shi zhongyu | null | 22 | m | 2 | 3 | 1 | song jiang | 25 | m | | 1 | shi zhongyu | null | 22 | m | 2 | 3 | 2 | zhang sanfeng | 25 | m | | 1 | shi zhongyu | null | 22 | m | 2 | 3 | 3 | miejue shitai | 25 | f | | 1 | shi zhongyu | null | 22 | m | 2 | 3 | 4 | lin chaoying | 25 | f | | 2 | shi potian | null | 22 | m | 1 | 7 | 1 | song jiang | 25 | m | | 2 | shi potian | null | 22 | m | 1 | 7 | 2 | zhang sanfeng | 25 | m | | 2 | shi potian | null | 22 | m | 1 | 7 | 3 | miejue shitai | 25 | f | | 2 | shi potian | null | 22 | m | 1 | 7 | 4 | lin chaoying | 25 | f | | 3 | xie yanke | null | 53 | m | 2 | 16 | 1 | song jiang | 25 | m | | 3 | xie yanke | null | 53 | m | 2 | 16 | 2 | zhang sanfeng | 25 | m | | 3 | xie yanke | null | 53 | m | 2 | 16 | 3 | miejue shitai | 25 | f | | 3 | xie yanke | null | 53 | m | 2 | 16 | 4 | lin chaoying | 25 | f | | 4 | ding dian | null | 32 | m | 4 | 4 | 1 | song jiang | 25 | m | | 4 | ding dian | null | 32 | m | 4 | 4 | 2 | zhang sanfeng | 25 | m | | 4 | ding dian | null | 32 | m | 4 | 4 | 3 | miejue shitai | 25 | f | | 4 | ding dian | null | 32 | m | 4 | 4 | 4 | lin chaoying | 25 | f | | 5 | yu yutong | null | 26 | m | 3 | 1 | 1 | song jiang | 25 | m | | 5 | yu yutong | null | 26 | m | 3 | 1 | 2 | zhang sanfeng | 25 | m | | 5 | yu yutong | null | 26 | m | 3 | 1 | 3 | miejue shitai | 25 | f | | 5 | yu yutong | null | 26 | m | 3 | 1 | 4 | lin chaoying | 25 | f | | 6 | shi qing | null | 46 | m | 5 | null | 1 | song jiang | 25 | m | | 6 | shi qing | null | 46 | m | 5 | null | 2 | zhang sanfeng | 25 | m | | 6 | shi qing | null | 46 | m | 5 | null | 3 | miejue shitai | 25 | f | | 6 | shi qing | null | 46 | m | 5 | null | 4 | lin chaoying | 25 | f | | 7 | xi ren | null | 19 | f | 3 | null | 1 | song jiang | 25 | m | | 7 | xi ren | null | 19 | f | 3 | null | 2 | zhang sanfeng | 25 | m | | 7 | xi ren | null | 19 | f | 3 | null | 3 | miejue shitai | 25 | f | | 7 | xi ren | null | 19 | f | 3 | null | 4 | lin chaoying | 25 | f | | 8 | lin daiyu | null | 17 | f | 7 | null | 1 | song jiang | 25 | m | | 8 | lin daiyu | null | 17 | f | 7 | null | 2 | zhang sanfeng | 25 | m | | 8 | lin daiyu | null | 17 | f | 7 | null | 3 | miejue shitai | 25 | f | | 8 | lin daiyu | null | 17 | f | 7 | null | 4 | lin chaoying | 25 | f | | 9 | ren yingying | null | 20 | f | 6 | null | 1 | song jiang | 25 | m | | 9 | ren yingying | null | 20 | f | 6 | null | 2 | zhang sanfeng | 25 | m | | 9 | ren yingying | null | 20 | f | 6 | null | 3 | miejue shitai | 25 | f | | 9 | ren yingying | null | 20 | f | 6 | null | 4 | lin chaoying | 25 | f | | 10 | yue lingshan | null | 19 | f | 3 | null | 1 | song jiang | 25 | m | | 10 | yue lingshan | null | 19 | f | 3 | null | 2 | zhang sanfeng | 25 | m | | 10 | yue lingshan | null | 19 | f | 3 | null | 3 | miejue shitai | 25 | f | | 10 | yue lingshan | null | 19 | f | 3 | null | 4 | lin chaoying | 25 | f | | 11 | yuan chengzhi | null | 23 | m | 6 | null | 1 | song jiang | 25 | m | | 11 | yuan chengzhi | null | 23 | m | 6 | null | 2 | zhang sanfeng | 25 | m | | 11 | yuan chengzhi | null | 23 | m | 6 | null | 3 | miejue shitai | 25 | f | | 11 | yuan chengzhi | null | 23 | m | 6 | null | 4 | lin chaoying | 25 | f | | 12 | wen qingqing | null | 19 | f | 1 | null | 1 | song jiang | 25 | m | | 12 | wen qingqing | null | 19 | f | 1 | null | 2 | zhang sanfeng | 25 | m | | 12 | wen qingqing | null | 19 | f | 1 | null | 3 | miejue shitai | 25 | f | | 12 | wen qingqing | null | 19 | f | 1 | null | 4 | lin chaoying | 25 | f | | 13 | tian boguang | null | 33 | m | 2 | null | 1 | song jiang | 25 | m | | 13 | tian boguang | null | 33 | m | 2 | null | 2 | zhang sanfeng | 25 | m | | 13 | tian boguang | null | 33 | m | 2 | null | 3 | miejue shitai | 25 | f | | 13 | tian boguang | null | 33 | m | 2 | null | 4 | lin chaoying | 25 | f | | 14 | lu wushuang | null | 17 | f | 3 | null | 1 | song jiang | 25 | m | | 14 | lu wushuang | null | 17 | f | 3 | null | 2 | zhang sanfeng | 25 | m | | 14 | lu wushuang | null | 17 | f | 3 | null | 3 | miejue shitai | 25 | f | | 14 | lu wushuang | null | 17 | f | 3 | null | 4 | lin chaoying | 25 | f | | 15 | duan yu | null | 19 | m | 4 | null | 1 | song jiang | 25 | m | | 15 | duan yu | null | 19 | m | 4 | null | 2 | zhang sanfeng | 25 | m | | 15 | duan yu | null | 19 | m | 4 | null | 3 | miejue shitai | 25 | f | | 15 | duan yu | null | 19 | m | 4 | null | 4 | lin chaoying | 25 | f | | 16 | xu zhu | null | 21 | m | 1 | null | 1 | song jiang | 25 | m | | 16 | xu zhu | null | 21 | m | 1 | null | 2 | zhang sanfeng | 25 | m | | 16 | xu zhu | null | 21 | m | 1 | null | 3 | miejue shitai | 25 | f | | 16 | xu zhu | null | 21 | m | 1 | null | 4 | lin chaoying | 25 | f | | 17 | lin chong | null | 25 | m | 4 | null | 1 | song jiang | 25 | m | | 17 | lin chong | null | 25 | m | 4 | null | 2 | zhang sanfeng | 25 | m | | 17 | lin chong | null | 25 | m | 4 | null | 3 | miejue shitai | 25 | f | | 17 | lin chong | null | 25 | m | 4 | null | 4 | lin chaoying | 25 | f | | 18 | hua rong | null | 23 | m | 7 | null | 1 | song jiang | 25 | m | | 18 | hua rong | null | 23 | m | 7 | null | 2 | zhang sanfeng | 25 | m | | 18 | hua rong | null | 23 | m | 7 | null | 3 | miejue shitai | 25 | f | | 18 | hua rong | null | 23 | m | 7 | null | 4 | lin chaoying | 25 | f | | 19 | xue baochai | null | 18 | f | 6 | null | 1 | song jiang | 25 | m | | 19 | xue baochai | null | 18 | f | 6 | null | 2 | zhang sanfeng | 25 | m | | 19 | xue baochai | null | 18 | f | 6 | null | 3 | miejue shitai | 25 | f | | 19 | xue baochai | null | 18 | f | 6 | null | 4 | lin chaoying | 25 | f | | 20 | diao chan | null | 19 | f | 7 | null | 1 | song jiang | 25 | m | | 20 | diao chan | null | 19 | f | 7 | null | 2 | zhang sanfeng | 25 | m | | 20 | diao chan | null | 19 | f | 7 | null | 3 | miejue shitai | 25 | f | | 20 | diao chan | null | 19 | f | 7 | null | 4 | lin chaoying | 25 | f | | 21 | huang yueying | null | 22 | f | 6 | null | 1 | song jiang | 25 | m | | 21 | huang yueying | null | 22 | f | 6 | null | 2 | zhang sanfeng | 25 | m | | 21 | huang yueying | null | 22 | f | 6 | null | 3 | miejue shitai | 25 | f | | 21 | huang yueying | null | 22 | f | 6 | null | 4 | lin chaoying | 25 | f | | 22 | xiao qiao | null | 20 | f | 1 | null | 1 | song jiang | 25 | m | | 22 | xiao qiao | null | 20 | f | 1 | null | 2 | zhang sanfeng | 25 | m | | 22 | xiao qiao | null | 20 | f | 1 | null | 3 | miejue shitai | 25 | f | | 22 | xiao qiao | null | 20 | f | 1 | null | 4 | lin chaoying | 25 | f | | 23 | ma chao | null | 23 | m | 4 | null | 1 | song jiang | 25 | m | | 23 | ma chao | null | 23 | m | 4 | null | 2 | zhang sanfeng | 25 | m | | 23 | ma chao | null | 23 | m | 4 | null | 3 | miejue shitai | 25 | f | | 23 | ma chao | null | 23 | m | 4 | null | 4 | lin chaoying | 25 | f | | 24 | xu xian | null | 27 | m | null | null | 1 | song jiang | 25 | m | | 24 | xu xian | null | 27 | m | null | null | 2 | zhang sanfeng | 25 | m | | 24 | xu xian | null | 27 | m | null | null | 3 | miejue shitai | 25 | f | | 24 | xu xian | null | 27 | m | null | null | 4 | lin chaoying | 25 | f | | 25 | sun dasheng | null | 100 | m | null | null | 1 | song jiang | 25 | m | | 25 | sun dasheng | null | 100 | m | null | null | 2 | zhang sanfeng | 25 | m | | 25 | sun dasheng | null | 100 | m | null | null | 3 | miejue shitai | 25 | f | | 25 | sun dasheng | null | 100 | m | null | null | 4 | lin chaoying | 25 | f | | 26 | xietingfeng | null | 23 | m | 2 | 1 | 1 | song jiang | 25 | m | | 26 | xietingfeng | null | 23 | m | 2 | 1 | 2 | zhang sanfeng | 25 | m | | 26 | xietingfeng | null | 23 | m | 2 | 1 | 3 | miejue shitai | 25 | f | | 26 | xietingfeng | null | 23 | m | 2 | 1 | 4 | lin chaoying | 25 | f | | 27 | liudehua | null | 18 | f | 1 | null | 1 | song jiang | 25 | m | | 27 | liudehua | null | 18 | f | 1 | null | 2 | zhang sanfeng | 25 | m | | 27 | liudehua | null | 18 | f | 1 | null | 3 | miejue shitai | 25 | f | | 27 | liudehua | null | 18 | f | 1 | null | 4 | lin chaoying | 25 | f | | 28 | mahuateng | null | 20 | m | 3 | null | 1 | song jiang | 25 | m | | 28 | mahuateng | null | 20 | m | 3 | null | 2 | zhang sanfeng | 25 | m | | 28 | mahuateng | null | 20 | m | 3 | null | 3 | miejue shitai | 25 | f | | 28 | mahuateng | null | 20 | m | 3 | null | 4 | lin chaoying | 25 | f | | 29 | wuyanzu | null | 19 | m | 4 | null | 1 | song jiang | 25 | m | | 29 | wuyanzu | null | 19 | m | 4 | null | 2 | zhang sanfeng | 25 | m | | 29 | wuyanzu | null | 19 | m | 4 | null | 3 | miejue shitai | 25 | f | | 29 | wuyanzu | null | 19 | m | 4 | null | 4 | lin chaoying | 25 | f | | 30 | wuzetian | null | 21 | f | null | null | 1 | song jiang | 25 | m | | 30 | wuzetian | null | 21 | f | null | null | 2 | zhang sanfeng | 25 | m | | 30 | wuzetian | null | 21 | f | null | null | 3 | miejue shitai | 25 | f | | 30 | wuzetian | null | 21 | f | null | null | 4 | lin chaoying | 25 | f | | 31 | song jiang | null | 18 | m | 45 | null | 1 | song jiang | 25 | m | | 31 | song jiang | null | 18 | m | 45 | null | 2 | zhang sanfeng | 25 | m | | 31 | song jiang | null | 18 | m | 45 | null | 3 | miejue shitai | 25 | f | | 31 | song jiang | null | 18 | m | 45 | null | 4 | lin chaoying | 25 | f | | 32 | zhang sanfeng | null | 18 | m | 94 | null | 1 | song jiang | 25 | m | | 32 | zhang sanfeng | null | 18 | m | 94 | null | 2 | zhang sanfeng | 25 | m | | 32 | zhang sanfeng | null | 18 | m | 94 | null | 3 | miejue shitai | 25 | f | | 32 | zhang sanfeng | null | 18 | m | 94 | null | 4 | lin chaoying | 25 | f | | 33 | miejue shitai | null | 18 | f | 77 | null | 1 | song jiang | 25 | m | | 33 | miejue shitai | null | 18 | f | 77 | null | 2 | zhang sanfeng | 25 | m | | 33 | miejue shitai | null | 18 | f | 77 | null | 3 | miejue shitai | 25 | f | | 33 | miejue shitai | null | 18 | f | 77 | null | 4 | lin chaoying | 25 | f | | 34 | lin chaoying | null | 18 | f | 93 | null | 1 | song jiang | 25 | m | | 34 | lin chaoying | null | 18 | f | 93 | null | 2 | zhang sanfeng | 25 | m | | 34 | lin chaoying | null | 18 | f | 93 | null | 3 | miejue shitai | 25 | f | | 34 | lin chaoying | null | 18 | f | 93 | null | 4 | lin chaoying | 25 | f | | 38 | abc | null | 20 | m | null | null | 1 | song jiang | 25 | m | | 38 | abc | null | 20 | m | null | null | 2 | zhang sanfeng | 25 | m | | 38 | abc | null | 20 | m | null | null | 3 | miejue shitai | 25 | f | | 38 | abc | null | 20 | m | null | null | 4 | lin chaoying | 25 | f | +-------+---------------+-------+-----+--------+---------+-----------+-----+---------------+-----+--------+ 140 rows in set (0.00 sec) 第一张表 0 1 2 3 1 2 3 4 第二张表 1 3 4 5 2 3 4 5 交叉连接后结果: 0 1 2 3 1 3 4 5 0 1 2 3 2 3 4 5 1 2 3 4 1 3 4 5 1 2 3 4 2 3 4 5 两张表换下位置不影响数据只是显示效果变了而已: 1 3 4 5 0 1 2 3 1 3 4 5 1 2 3 4 2 3 4 5 0 1 2 3 2 3 4 5 1 2 3 4 mariadb [hellodb]> select * from teachers , s1; (这个命令也可以交叉连接但是比较老了推荐使用第一种) +-----+---------------+-----+--------+-------+---------------+-------+-----+--------+---------+-----------+ | tid | name | age | gender | stuid | name | phone | age | gender | classid | teacherid | +-----+---------------+-----+--------+-------+---------------+-------+-----+--------+---------+-----------+ | 1 | song jiang | 25 | m | 1 | shi zhongyu | null | 22 | m | 2 | 3 | | 2 | zhang sanfeng | 25 | m | 1 | shi zhongyu | null | 22 | m | 2 | 3 | | 3 | miejue shitai | 25 | f | 1 | shi zhongyu | null | 22 | m | 2 | 3 | | 4 | lin chaoying | 25 | f | 1 | shi zhongyu | null | 22 | m | 2 | 3 | | 1 | song jiang | 25 | m | 2 | shi potian | null | 22 | m | 1 | 7 | | 2 | zhang sanfeng | 25 | m | 2 | shi potian | null | 22 | m | 1 | 7 | | 3 | miejue shitai | 25 | f | 2 | shi potian | null | 22 | m | 1 | 7 | | 4 | lin chaoying | 25 | f | 2 | shi potian | null | 22 | m | 1 | 7 | | 1 | song jiang | 25 | m | 3 | xie yanke | null | 53 | m | 2 | 16 | | 2 | zhang sanfeng | 25 | m | 3 | xie yanke | null | 53 | m | 2 | 16 | | 3 | miejue shitai | 25 | f | 3 | xie yanke | null | 53 | m | 2 | 16 | | 4 | lin chaoying | 25 | f | 3 | xie yanke | null | 53 | m | 2 | 16 |
- 挑出两张表的个别字段。
mariadb [hellodb]> select name,age,gender from teachers cross join s1; (这里有两个字段是重复的name,age两个表都有) error 1052 (23000): column 'name' in field list is ambiguous mariadb [hellodb]> select stuid,s1.name,tid,teachers.name from teachers cross join s1; (分别指定是哪个表的name) +-------+---------------+-----+---------------+ | stuid | name | tid | name | +-------+---------------+-----+---------------+ | 1 | shi zhongyu | 1 | song jiang | | 1 | shi zhongyu | 2 | zhang sanfeng | | 1 | shi zhongyu | 3 | miejue shitai | | 1 | shi zhongyu | 4 | lin chaoying | | 2 | shi potian | 1 | song jiang | | 2 | shi potian | 2 | zhang sanfeng | | 2 | shi potian | 3 | miejue shitai | | 2 | shi potian | 4 | lin chaoying | | 3 | xie yanke | 1 | song jiang | | 3 | xie yanke | 2 | zhang sanfeng | | 3 | xie yanke | 3 | miejue shitai | | 3 | xie yanke | 4 | lin chaoying | | 4 | ding dian | 1 | song jiang | | 4 | ding dian | 2 | zhang sanfeng | | 4 | ding dian | 3 | miejue shitai | | 4 | ding dian | 4 | lin chaoying | | 5 | yu yutong | 1 | song jiang | | 5 | yu yutong | 2 | zhang sanfeng | | 5 | yu yutong | 3 | miejue shitai | | 5 | yu yutong | 4 | lin chaoying | | 6 | shi qing | 1 | song jiang | | 6 | shi qing | 2 | zhang sanfeng | | 6 | shi qing | 3 | miejue shitai | | 6 | shi qing | 4 | lin chaoying | | 7 | xi ren | 1 | song jiang | | 7 | xi ren | 2 | zhang sanfeng | | 7 | xi ren | 3 | miejue shitai | | 7 | xi ren | 4 | lin chaoying | | 8 | lin daiyu | 1 | song jiang | | 8 | lin daiyu | 2 | zhang sanfeng | | 8 | lin daiyu | 3 | miejue shitai | | 8 | lin daiyu | 4 | lin chaoying | | 9 | ren yingying | 1 | song jiang | | 9 | ren yingying | 2 | zhang sanfeng | | 9 | ren yingying | 3 | miejue shitai | | 9 | ren yingying | 4 | lin chaoying | | 10 | yue lingshan | 1 | song jiang | | 10 | yue lingshan | 2 | zhang sanfeng | | 10 | yue lingshan | 3 | miejue shitai | | 10 | yue lingshan | 4 | lin chaoying | | 11 | yuan chengzhi | 1 | song jiang | | 11 | yuan chengzhi | 2 | zhang sanfeng | | 11 | yuan chengzhi | 3 | miejue shitai | | 11 | yuan chengzhi | 4 | lin chaoying | (省略了太长) mariadb [hellodb]> select stuid,s1.name,s1.age,tid,teachers.name,teachers.age from teachers cross join s1; (name和age都可以加在里面指定) +-------+---------------+-----+-----+---------------+-----+ | stuid | name | age | tid | name | age | +-------+---------------+-----+-----+---------------+-----+ | 1 | shi zhongyu | 22 | 1 | song jiang | 25 | | 1 | shi zhongyu | 22 | 2 | zhang sanfeng | 25 | | 1 | shi zhongyu | 22 | 3 | miejue shitai | 25 | | 1 | shi zhongyu | 22 | 4 | lin chaoying | 25 | | 2 | shi potian | 22 | 1 | song jiang | 25 | | 2 | shi potian | 22 | 2 | zhang sanfeng | 25 | | 2 | shi potian | 22 | 3 | miejue shitai | 25 | | 2 | shi potian | 22 | 4 | lin chaoying | 25 | | 3 | xie yanke | 53 | 1 | song jiang | 25 | | 3 | xie yanke | 53 | 2 | zhang sanfeng | 25 | | 3 | xie yanke | 53 | 3 | miejue shitai | 25 | | 3 | xie yanke | 53 | 4 | lin chaoying | 25 | | 4 | ding dian | 32 | 1 | song jiang | 25 | | 4 | ding dian | 32 | 2 | zhang sanfeng | 25 | | 4 | ding dian | 32 | 3 | miejue shitai | 25 | | 4 | ding dian | 32 | 4 | lin chaoying | 25 | | 5 | yu yutong | 26 | 1 | song jiang | 25 | | 5 | yu yutong | 26 | 2 | zhang sanfeng | 25 | | 5 | yu yutong | 26 | 3 | miejue shitai | 25 | | 5 | yu yutong | 26 | 4 | lin chaoying | 25 | (省略了太长) mariadb [hellodb]> select stuid,s1.name as s1_name,tid,teachers.name as teachers_name from teachers cross join s1; (也可以加上别名来区分比较清晰) +-------+---------------+-----+---------------+ | stuid | s1_name | tid | teachers_name | +-------+---------------+-----+---------------+ | 1 | shi zhongyu | 1 | song jiang | | 1 | shi zhongyu | 2 | zhang sanfeng | | 1 | shi zhongyu | 3 | miejue shitai | | 1 | shi zhongyu | 4 | lin chaoying | | 2 | shi potian | 1 | song jiang | | 2 | shi potian | 2 | zhang sanfeng | | 2 | shi potian | 3 | miejue shitai | | 2 | shi potian | 4 | lin chaoying | | 3 | xie yanke | 1 | song jiang | | 3 | xie yanke | 2 | zhang sanfeng | | 3 | xie yanke | 3 | miejue shitai | | 3 | xie yanke | 4 | lin chaoying | (省略)
- 对表起别名利用表的别名来查询数据。(定义别名是在字段的最后用在前面用,只在当前字段生效)
mariadb [hellodb]> select stuid,s.name as s1_name,s.age,t.name as teachers_name,t.age from teachers t cross join s1 s; +-------+---------------+-----+---------------+-----+ | stuid | s1_name | age | teachers_name | age | +-------+---------------+-----+---------------+-----+ | 1 | shi zhongyu | 22 | song jiang | 25 | | 1 | shi zhongyu | 22 | zhang sanfeng | 25 | | 1 | shi zhongyu | 22 | miejue shitai | 25 | | 1 | shi zhongyu | 22 | lin chaoying | 25 | | 2 | shi potian | 22 | song jiang | 25 | | 2 | shi potian | 22 | zhang sanfeng | 25 | | 2 | shi potian | 22 | miejue shitai | 25 | | 2 | shi potian | 22 | lin chaoying | 25 | | 3 | xie yanke | 53 | song jiang | 25 | | 3 | xie yanke | 53 | zhang sanfeng | 25 | | 3 | xie yanke | 53 | miejue shitai | 25 | | 3 | xie yanke | 53 | lin chaoying | 25 | | 4 | ding dian | 32 | song jiang | 25 | | 4 | ding dian | 32 | zhang sanfeng | 25 | | 4 | ding dian | 32 | miejue shitai | 25 | | 4 | ding dian | 32 | lin chaoying | 25 | mariadb [hellodb]> select stuid,s1.name s1_name,s1.age,tid,t.name teacher_name,teachers.age from teachers t cross join s1; (别名定义之后不能使用原始名字) error 1054 (42s22): unknown column 'teachers.age' in 'field list'
内连接
等值连接:让表之间的字段以“等值”建立连接关系
- 把两个表有交集的地方连接起来
mariadb [hellodb]> select * from s1 inner join t1 on s1.teacherid=t1.tid; (三个个老师各教一个学生) +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ | stuid | name | age | gender | classid | teacherid | tid | name | age | gender | +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ | 5 | yu yutong | 26 | m | 3 | 1 | 1 | song jiang | 45 | m | | 1 | shi zhongyu | 22 | m | 2 | 3 | 3 | miejue shitai | 77 | f | | 4 | ding dian | 32 | m | 4 | 4 | 4 | lin chaoying | 93 | f | +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ 3 rows in set (0.00 sec) mariadb [hellodb]> update s1 set teacherid=1 where stuid=25; (修改一下s1表的teacherid的值为1) query ok, 1 row affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0 mariadb [hellodb]> select * from s1 inner join t1 on s1.teacherid=t1.tid; (再次使用这条命令,查看就是songjiang教两个学生) +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ | stuid | name | age | gender | classid | teacherid | tid | name | age | gender | +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ | 5 | yu yutong | 26 | m | 3 | 1 | 1 | song jiang | 45 | m | | 25 | sun dasheng | 100 | m | null | 1 | 1 | song jiang | 45 | m | | 1 | shi zhongyu | 22 | m | 2 | 3 | 3 | miejue shitai | 77 | f | | 4 | ding dian | 32 | m | 4 | 4 | 4 | lin chaoying | 93 | f | +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ 4 rows in set (0.00 sec) mariadb [hellodb]> select * from s1 , t1 where s1.teacherid=t1.tid; (不加 inner join 的老写法) +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ | stuid | name | age | gender | classid | teacherid | tid | name | age | gender | +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ | 5 | yu yutong | 26 | m | 3 | 1 | 1 | song jiang | 45 | m | | 25 | sun dasheng | 100 | m | null | 1 | 1 | song jiang | 45 | m | | 1 | shi zhongyu | 22 | m | 2 | 3 | 3 | miejue shitai | 77 | f | | 4 | ding dian | 32 | m | 4 | 4 | 4 | lin chaoying | 93 | f | +-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+ 4 rows in set (0.01 sec)
- 内连接之后过滤:先连接再过滤,显示s1表大于30的人。
mariadb [hellodb]> select * from s1 inner join t1 on s1.teacherid=t1.tid and s1.age >30; +-------+-------------+-----+--------+---------+-----------+-----+--------------+-----+--------+ | stuid | name | age | gender | classid | teacherid | tid | name | age | gender | +-------+-------------+-----+--------+---------+-----------+-----+--------------+-----+--------+ | 25 | sun dasheng | 100 | m | null | 1 | 1 | song jiang | 45 | m | | 4 | ding dian | 32 | m | 4 | 4 | 4 | lin chaoying | 93 | f | +-------+-------------+-----+--------+---------+-----------+-----+--------------+-----+--------+ 2 rows in set (0.00 sec)
- 查询完之后过滤
mariadb [hellodb]> select * from s1 inner join t1 on s1.teacherid=t1.tid where s1.a +-------+-------------+-----+--------+---------+-----------+-----+--------------+-- | stuid | name | age | gender | classid | teacherid | tid | name | a +-------+-------------+-----+--------+---------+-----------+-----+--------------+-- | 25 | sun dasheng | 100 | m | null | 1 | 1 | song jiang | | 4 | ding dian | 32 | m | 4 | 4 | 4 | lin chaoying | +-------+-------------+-----+--------+---------+-----------+-----+--------------+-- 2 rows in set (0.00 sec)
外连接
外连接: 左外连接:from tb1 left join tb2 on tb1.col=tb2.col (排在前面的) 右外连接:from tb1 right join tb2 on tb1.col=tb2.col (排在后面的)
左外连接 left outer
- 学生表全留下来老师的只留下来有交集的地方。 (没有交集的地方空值代替)
mariadb [hellodb]> select * from s1 left outer join t1 on s1.teacherid=t1.tid; +-------+---------------+-----+--------+---------+-----------+------+---------------+------+--------+ | stuid | name | age | gender | classid | teacherid | tid | name | age | gender | +-------+---------------+-----+--------+---------+-----------+------+---------------+------+--------+ | 1 | shi zhongyu | 22 | m | 2 | 3 | 3 | miejue shitai | 77 | f | | 2 | shi potian | 22 | m | 1 | 7 | null | null | null | null | | 3 | xie yanke | 53 | m | 2 | 16 | null | null | null | null | | 4 | ding dian | 32 | m | 4 | 4 | 4 | lin chaoying | 93 | f | | 5 | yu yutong | 26 | m | 3 | 1 | 1 | song jiang | 45 | m | | 6 | shi qing | 46 | m | 5 | null | null | null | null | null | | 7 | xi ren | 19 | f | 3 | null | null | null | null | null | | 8 | lin daiyu | 17 | f | 7 | null | null | null | null | null | | 9 | ren yingying | 20 | f | 6 | null | null | null | null | null | | 10 | yue lingshan | 19 | f | 3 | null | null | null | null | null | | 11 | yuan chengzhi | 23 | m | 6 | null | null | null | null | null | | 12 | wen qingqing | 19 | f | 1 | null | null | null | null | null | | 13 | tian boguang | 33 | m | 2 | null | null | null | null | null | | 14 | lu wushuang | 17 | f | 3 | null | null | null | null | null | | 15 | duan yu | 19 | m | 4 | null | null | null | null | null | | 16 | xu zhu | 21 | m | 1 | null | null | null | null | null | | 17 | lin chong | 25 | m | 4 | null | null | null | null | null | | 18 | hua rong | 23 | m | 7 | null | null | null | null | null | | 19 | xue baochai | 18 | f | 6 | null | null | null | null | null | | 20 | diao chan | 19 | f | 7 | null | null | null | null | null | | 21 | huang yueying | 22 | f | 6 | null | null | null | null | null | | 22 | xiao qiao | 20 | f | 1 | null | null | null | null | null | | 23 | ma chao | 23 | m | 4 | null | null | null | null | null | | 24 | xu xian | 27 | m | null | null | null | null | null | null | | 25 | sun dasheng | 100 | m | null | 1 | 1 | song jiang | 45 | m | +-------+---------------+-----+--------+---------+-----------+------+---------------+------+--------+ 25 rows in set (0.00 sec)
- 左外连接扩展用法
- 取出没有老师教的学生 (用where指定)
mariadb [hellodb]> select * from t1; (教师表) +-----+---------------+-----+--------+ | tid | name | age | gender | +-----+---------------+-----+--------+ | 1 | song jiang | 45 | m | | 2 | zhang sanfeng | 94 | m | | 3 | miejue shitai | 77 | f | | 4 | lin chaoying | 93 | f | +-----+---------------+-----+--------+ 4 rows in set (0.00 sec) mariadb [hellodb]> select * from s1 left outer join t1 on s1.teacherid=t1.tid where tid is null; (前面两个,老师表里没有这两个老师的编号) +-------+---------------+-----+--------+---------+-----------+------+------+------+--------+ | stuid | name | age | gender | classid | teacherid | tid | name | age | gender | +-------+---------------+-----+--------+---------+-----------+------+------+------+--------+ | 2 | shi potian | 22 | m | 1 | 7 | null | null | null | null | | 3 | xie yanke | 53 | m | 2 | 16 | null | null | null | null | | 6 | shi qing | 46 | m | 5 | null | null | null | null | null | | 7 | xi ren | 19 | f | 3 | null | null | null | null | null | | 8 | lin daiyu | 17 | f | 7 | null | null | null | null | null | | 9 | ren yingying | 20 | f | 6 | null | null | null | null | null | | 10 | yue lingshan | 19 | f | 3 | null | null | null | null | null | | 11 | yuan chengzhi | 23 | m | 6 | null | null | null | null | null | | 12 | wen qingqing | 19 | f | 1 | null | null | null | null | null | | 13 | tian boguang | 33 | m | 2 | null | null | null | null | null | | 14 | lu wushuang | 17 | f | 3 | null | null | null | null | null | | 15 | duan yu | 19 | m | 4 | null | null | null | null | null | | 16 | xu zhu | 21 | m | 1 | null | null | null | null | null | | 17 | lin chong | 25 | m | 4 | null | null | null | null | null | | 18 | hua rong | 23 | m | 7 | null | null | null | null | null | | 19 | xue baochai | 18 | f | 6 | null | null | null | null | null | | 20 | diao chan | 19 | f | 7 | null | null | null | null | null | | 21 | huang yueying | 22 | f | 6 | null | null | null | null | null | | 22 | xiao qiao | 20 | f | 1 | null | null | null | null | null | | 23 | ma chao | 23 | m | 4 | null | null | null | null | null | | 24 | xu xian | 27 | m | null | null | null | null | null | null | +-------+---------------+-----+--------+---------+-----------+------+------+------+--------+ 21 rows in set (0.00 sec)
右外链接:right outer
- 老师表全留下来,学生表有交集的地方留下来。
mariadb [hellodb]> select * from s1 right outer join t1 on s1.teacherid=t1.tid; +-------+-------------+------+--------+---------+-----------+-----+---------------+-----+--------+ | stuid | name | age | gender | classid | teacherid | tid | name | age | gender | +-------+-------------+------+--------+---------+-----------+-----+---------------+-----+--------+ | 1 | shi zhongyu | 22 | m | 2 | 3 | 3 | miejue shitai | 77 | f | | 4 | ding dian | 32 | m | 4 | 4 | 4 | lin chaoying | 93 | f | | 5 | yu yutong | 26 | m | 3 | 1 | 1 | song jiang | 45 | m | | 25 | sun dasheng | 100 | m | null | 1 | 1 | song jiang | 45 | m | | null | null | null | null | null | null | 2 | zhang sanfeng | 94 | m | +-------+-------------+------+--------+---------+-----------+-----+---------------+-----+--------+ 5 rows in set (0.00 sec)
- 右外连接扩展用法
- 没有教学生的老师留下来。 (和左外连接的逻辑是一样的)
mariadb [hellodb]> select * from s1 right outer join t1 on s1.teacherid=t1.tid where s1.teacherid is null; +-------+------+------+--------+---------+-----------+-----+---------------+-----+--------+ | stuid | name | age | gender | classid | teacherid | tid | name | age | gender | +-------+------+------+--------+---------+-----------+-----+---------------+-----+--------+ | null | null | null | null | null | null | 2 | zhang sanfeng | 94 | m | +-------+------+------+--------+---------+-----------+-----+---------------+-----+--------+ 1 row in set (0.00 sec)
完全外连接
和交叉连接不一样 mysql不支持full outer join
- 把左外连接和右外连接用union联合起来,有交集的地方对应,没有交集的也地方输出出来。
mariadb [hellodb]> select * from s1 left outer join t1 on s1.teacherid=t1.tid -> union -> select * from s1 right outer join t1 on s1.teacherid=t1.tid; +-------+---------------+------+--------+---------+-----------+------+---------------+------+--------+ | stuid | name | age | gender | classid | teacherid | tid | name | age | gender | +-------+---------------+------+--------+---------+-----------+------+---------------+------+--------+ | 1 | shi zhongyu | 22 | m | 2 | 3 | 3 | miejue shitai | 77 | f | | 2 | shi potian | 22 | m | 1 | 7 | null | null | null | null | | 3 | xie yanke | 53 | m | 2 | 16 | null | null | null | null | | 4 | ding dian | 32 | m | 4 | 4 | 4 | lin chaoying | 93 | f | | 5 | yu yutong | 26 | m | 3 | 1 | 1 | song jiang | 45 | m | | 6 | shi qing | 46 | m | 5 | null | null | null | null | null | | 7 | xi ren | 19 | f | 3 | null | null | null | null | null | | 8 | lin daiyu | 17 | f | 7 | null | null | null | null | null | | 9 | ren yingying | 20 | f | 6 | null | null | null | null | null | | 10 | yue lingshan | 19 | f | 3 | null | null | null | null | null | | 11 | yuan chengzhi | 23 | m | 6 | null | null | null | null | null | | 12 | wen qingqing | 19 | f | 1 | null | null | null | null | null | | 13 | tian boguang | 33 | m | 2 | null | null | null | null | null | | 14 | lu wushuang | 17 | f | 3 | null | null | null | null | null | | 15 | duan yu | 19 | m | 4 | null | null | null | null | null | | 16 | xu zhu | 21 | m | 1 | null | null | null | null | null | | 17 | lin chong | 25 | m | 4 | null | null | null | null | null | | 18 | hua rong | 23 | m | 7 | null | null | null | null | null | | 19 | xue baochai | 18 | f | 6 | null | null | null | null | null | | 20 | diao chan | 19 | f | 7 | null | null | null | null | null | | 21 | huang yueying | 22 | f | 6 | null | null | null | null | null | | 22 | xiao qiao | 20 | f | 1 | null | null | null | null | null | | 23 | ma chao | 23 | m | 4 | null | null | null | null | null | | 24 | xu xian | 27 | m | null | null | null | null | null | null | | 25 | sun dasheng | 100 | m | null | 1 | 1 | song jiang | 45 | m | | null | null | null | null | null | null | 2 | zhang sanfeng | 94 | m | +-------+---------------+------+--------+---------+-----------+------+---------------+------+--------+ 26 rows in set (0.00 sec)
- 完全外连接扩展用法:有交集的地方去除掉,只留外连接。
- 把没有老师的学生,和没有学生的老师取出来。
mariadb [hellodb]> select * from (select s.stuid,s.name s_name,s.teacherid,t.tid,t.name t_name from s1 s left outer join t1 t on s.teacherid=t.tid union select s.stuid,s.name,s.teacherid,t.tid,t.name from s1 s right outer join t1 t on s.teacherid=t.tid) as a where a.teacherid is null or a.tid is null; +-------+---------------+-----------+------+---------------+ | stuid | s_name | teacherid | tid | t_name | +-------+---------------+-----------+------+---------------+ | 2 | shi potian | 7 | null | null | | 3 | xie yanke | 16 | null | null | | 6 | shi qing | null | null | null | | 7 | xi ren | null | null | null | | 8 | lin daiyu | null | null | null | | 9 | ren yingying | null | null | null | | 10 | yue lingshan | null | null | null | | 11 | yuan chengzhi | null | null | null | | 12 | wen qingqing | null | null | null | | 13 | tian boguang | null | null | null | | 14 | lu wushuang | null | null | null | | 15 | duan yu | null | null | null | | 16 | xu zhu | null | null | null | | 17 | lin chong | null | null | null | | 18 | hua rong | null | null | null | | 19 | xue baochai | null | null | null | | 20 | diao chan | null | null | null | | 21 | huang yueying | null | null | null | | 22 | xiao qiao | null | null | null | | 23 | ma chao | null | null | null | | 24 | xu xian | null | null | null | | null | null | null | 2 | zhang sanfeng | +-------+---------------+-----------+------+---------------+ 22 rows in set (0.00 sec)
三张表查询
取学生姓名,成绩,科目。
- 先取出学生的姓名和成绩 (分两步做思路清晰一点)
mariadb [hellodb]> select st.name,sc.courseid,sc.score from s1 st inner join scores sc on st.stuid=sc.stuid; +-------------+----------+-------+ | name | courseid | score | +-------------+----------+-------+ | shi zhongyu | 2 | 77 | | shi zhongyu | 6 | 93 | | shi potian | 2 | 47 | | shi potian | 5 | 97 | | xie yanke | 2 | 88 | | xie yanke | 6 | 75 | | ding dian | 5 | 71 | | ding dian | 2 | 89 | | yu yutong | 1 | 39 | | yu yutong | 7 | 63 | | shi qing | 1 | 96 | | xi ren | 1 | 86 | | xi ren | 7 | 83 | | lin daiyu | 4 | 57 | | lin daiyu | 3 | 93 | +-------------+----------+-------+ 15 rows in set (0.00 sec)
- 在连接一次取出学生姓名,成绩,科目。
mariadb [hellodb]> select st.name,co.course,sc.score from s1 st inner join scores sc on st.stuid=sc.stuid inner join courses co on sc.courseid=co.courseid; +-------------+----------------+-------+ | name | course | score | +-------------+----------------+-------+ | shi zhongyu | kuihua baodian | 77 | | shi zhongyu | weituo zhang | 93 | | shi potian | kuihua baodian | 47 | | shi potian | daiyu zanghua | 97 | | xie yanke | kuihua baodian | 88 | | xie yanke | weituo zhang | 75 | | ding dian | daiyu zanghua | 71 | | ding dian | kuihua baodian | 89 | | yu yutong | hamo gong | 39 | | yu yutong | dagou bangfa | 63 | | shi qing | hamo gong | 96 | | xi ren | hamo gong | 86 | | xi ren | dagou bangfa | 83 | | lin daiyu | taiji quan | 57 | | lin daiyu | jinshe jianfa | 93 | +-------------+----------------+-------+ 15 rows in set (0.00 sec)
严禁出现四张表join的情况
自连接
自联结顾名思义就是把一张表假设为两张一样的表,然后在做“多表查询”
- 先构建一张表
mariadb [hellodb]> create table emp (id int, name char(20),leaderid int); query ok, 0 rows affected (0.01 sec) mariadb [hellodb]> insert emp value(1,'huangshang',null); query ok, 1 row affected (0.00 sec) mariadb [hellodb]> insert emp value(2,'taihou','huangshang'); error 1366 (22007): incorrect integer value: 'huangshang' for column `hellodb`.`emp`.`leaderid` at row 1 mariadb [hellodb]> insert emp value(2,'taihou',1); query ok, 1 row affected (0.00 sec) mariadb [hellodb]> insert emp value(3,'guifei',2); query ok, 1 row affected (0.00 sec) mariadb [hellodb]> insert emp value(4,'shufei',3); query ok, 1 row affected (0.00 sec) mariadb [hellodb]> select * from emp; +------+------------+----------+ | id | name | leaderid | +------+------------+----------+ | 1 | huangshang | null | | 2 | taihou | 1 | | 3 | guifei | 2 | | 4 | shufei | 3 | +------+------------+----------+ 4 rows in set (0.00 sec)
- 查询表里的上级的姓名。
- 想象为两张表 员工表 和上司表 起别名做成
mariadb [hellodb]> select * from emp as e inner join emp as l on e.leaderid=l.id; +------+--------+----------+------+------------+----------+ | id | name | leaderid | id | name | leaderid | +------+--------+----------+------+------------+----------+ | 2 | taihou | 1 | 1 | huangshang | null | | 3 | guifei | 2 | 2 | taihou | 1 | | 4 | shufei | 3 | 3 | guifei | 2 | +------+--------+----------+------+------------+----------+ 3 rows in set (0.00 sec)
- 取出来对应的上级,但是缺失了最上级。
mariadb [hellodb]> select e.name emp,l.name leader from emp as e inner join emp as l on e.leaderid=l.id; +--------+------------+ | emp | leader | +--------+------------+ | taihou | huangshang | | guifei | taihou | | shufei | guifei | +--------+------------+ 3 rows in set (0.00 sec)
- 取出每个人对应的上级的id。
mariadb [hellodb]> select e.name emp,l.name leader from emp as e left join emp as l on e.leaderid=l.id; +------------+------------+ | emp | leader | +------------+------------+ | taihou | huangshang | | guifei | taihou | | shufei | guifei | | huangshang | null | +------------+------------+ 4 rows in set (0.00 sec)
sql语句的关键字执行顺序
上一篇: js生成条形码
下一篇: 让Wi-Fi加速不加价 腾达AC23上手