[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
多表的连表查询,查询方式可能主要就是下面这几种方式:
直接通过where语句将重叠的字段进行匹配就比如下面这种
select * from tablea,tableb where tablea.aid=tableb.bid;
另外一种就是通过内连接或者外连接的方式,就比如下面这种
select * from tablea LEFT JOIN tableb on tablea.aid=tableb.bid;
select * from tablea RIGHT JOIN tableb on tablea.aid=tableb.bid;
select * from tablea INNER JOIN tableb on tablea.aid=tableb.bid;
可能第一种通过where语句查询,大家都能理解.
但是不知道大家有没有去尝试过理解下面关于内连接与外连接查询方式的区别 ,接下来我们先通过一张图来演示一下连接的整个层级关系:
1. 内连接
select * from tableb INNER JOIN tablea on tablea.aid=tableb.bid;
这里我们通过与上面外连接的对比分析之后就可以看出来,内连接整个更加能够体现数据的完整性,上图我们可以看出
内连接只会显示所有匹配结果的数据;
那些不匹配的数据,不管是左表中的,还是右表中的都不显示
2. 外连接
2.1左连接
我们先通过两个例子来让大家看看实际的效果我们再来细讲:
select * from tablea LEFT JOIN tableb on tablea.aid=tableb.bid;
显然这里是以 tablea 的数据为基准的
看完这两个例子,想必大家也能够自己分析出来了,显然
永远是左表的数据是完整的,右表中只会查询出与左表匹配的数据,如果不匹配就不显示,显示为空.
整个过程都是以左表为基准的
2.2右连接
我们也是通过两个例子来细讲:
select * from tablea RIGHT JOIN tableb on tablea.aid=tableb.bid;
显然这里是以 tableb 的数据为基准的
看完这两个例子,想必大家也能够自己分析出来了,显然
永远是右表的数据是完整的,左表中只会查询出与右表匹配的数据,如果不匹配就不显示,显示为空.
整个过程都是以右表为基准的
到这里我们就要稍微区分一下基准表的定义,我们切不可将那个表名在前就觉得它是基准表,这里还是通过下面两张图:
左连接基准表
右连接基准表
所以基准表并不是以谁写在前面谁就是基准表定义的,还是通过位置来定义的,
左连接就以左边的表为基准表;右连接就以右边的表为基准