SQL语句如何实现超简单的多表查询
作者:简G 时间:2024-01-21 15:42:05
一、简单的多表联查(inner join,left join,right join)
1、 两表联查
user_table表
department表
1、inner join代表内连接,数据显示内容以外键为准,意思就是外键没有的,数据就不显示。
select user_table.id,user_table.username,user_table.sex,user_table.phone,user_table.address,department.dname
from user_table inner join department
on user_table.departmentid=department.did;
查询结果如下:
2、left join代表左连接,数据显示内容以左边表为准,意思就是不管右边表查出来是否有数据,左边表的数据有的一定会显示。
select user_table.id,user_table.username,user_table.sex,user_table.phone,user_table.address,department.dname
from user_table left join department
on user_table.departmentid=department.did;
3、right join代表右连接,数据显示内容以右边表为准,意思就是不管左边表查出来是否有数据,右边表的数据有的一定会显示。
select user_table.id,user_table.username,user_table.sex,user_table.phone,user_table.address,department.dname
from user_table right join department
on user_table.departmentid=department.did;
2、三表联查
只需要在sql语句后面继续加上inner join即可,当然这是以内连接为主。如下:
work表
department表后面添加work表的主键作为关联的外键
select user_table.id,user_table.username,user_table.sex,user_table.phone,user_table.address,department.dname,work.worktext
from user_table
inner join department
on user_table.departmentid=department.did
inner join work
on work.workid=department.workid;
二、触发器
触发器就是当对某个表执行某个操作的时候触发,可以有效防止恶意的sql注入。
来源:https://blog.csdn.net/weixin_45042272/article/details/110235123
标签:sql语句,多表,查询
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python List列表对象内置方法实例详解
2023-08-21 12:49:58
JS中FormData类实现文件上传
2024-04-10 10:50:10
![](https://img.aspxhome.com/file/2023/0/136920_0s.jpg)
HTML在线编辑器的原理分析(整理)
2007-12-22 10:25:00
pyqt5实现俄罗斯方块游戏
2023-07-21 11:02:32
![](https://img.aspxhome.com/file/2023/8/84118_0s.png)
关于Python Socket编程的要点详解
2021-11-25 17:44:11
![](https://img.aspxhome.com/file/2023/5/103285_0s.png)
python办公自动化之excel的操作
2023-11-20 14:01:01
python中print()函数的“,”与java中System.out.print()函数中的“+”功能详解
2021-12-18 10:27:02
Python使用for实现无限循环的多种方式汇总
2023-04-30 15:15:11
Python实现复制文档数据
2022-07-15 02:39:32
Node.js Koa2使用JWT进行鉴权的方法示例
2024-05-02 17:05:45
![](https://img.aspxhome.com/file/2023/5/129995_0s.png)
Python获取运行目录与当前脚本目录的方法
2023-12-19 13:17:59
不同情况下修复Access数据库的实用方法
2008-11-28 16:18:00
Mysql环境变量配置方式
2024-01-25 22:41:30
![](https://img.aspxhome.com/file/2023/8/122918_0s.png)
详解python中的装饰器
2023-02-20 10:33:36
python实现京东订单推送到测试环境,提供便利操作示例
2021-09-01 09:03:46
使用Python爬取最好大学网大学排名
2023-09-17 09:24:45
Pandas 按索引合并数据集的方法
2023-06-01 13:35:10
mysql数据库远程访问设置方法
2024-01-14 11:25:34
ASP怎样获得代码中第一张图片地址
2008-10-29 09:40:00
opencv resize图片为正方形尺寸的实现方法
2023-02-21 15:34:51
![](https://img.aspxhome.com/file/2023/0/92410_0s.jpg)