-
2019-09-18 14:22:33
SELECT * FROM A AS a LEFT JOIN B b1 ON a.bj_type=b1.bjType_id LEFT JOIN B b2 on a.dq_type=b2.bjType_id;
更多相关内容 -
oracle数据库中有多张表,如何查看其中两个表之间是否存在关联,以及他们关联的字段。
2017-07-19 08:43:40我目前用的是pl/sql对数据库进行操作,数据库中大概有几百张表,我想看看这些表之间有没有关联关系(我需要用到的两张表有没有关联关系以及是通过那些字段关联的)。我试着用网上的方法:新建一个diagram window,... -
laravel 多对多关联,明确定义出关联关系,自定义两表与关联表的关联字段
2018-05-16 10:51:34laravel的官方文档上面只介绍了...下面我来介绍在我们遇到两个表进行多对多关联,而且关联的时候,设置这两个表与中间表的关联字段。例如: a表结构为: id 主键, tag 标识, ………… b表结构为: ...laravel的官方文档上面只介绍了belongsToMany的4个参数,文档如下:
到这里为止,官方文档上面对于多对多关联介绍差不多完了,如果想查看更多请看官方文档。
下面我来介绍在我们遇到两个表进行多对多关联,而且关联的时候,设置这两个表与中间表的关联字段。
例如:
a表结构为:
id 主键,
tag 标识,
…………
b表结构为:
id 主键,
…………
a_b表结构为:
id 主键,
b_id,
a_tag
当我们遇到这种情况的时候,查看官方文档上并没有找到相关的处理方法,官方文档上面的介绍整体说下来 :
belongsToMany(关联表模型,中间表表名,中间表与当前模型的关联字段,中间表与关联表的关联字段);
如果我们上面的表结构按照这种方式关联,假设我们当前模型为a ; 那么关联写为 belongsToMany(b:class, 'a_b', 'a_tag', 'b_id');
如果我们这么写的话,产生出来的sql为:
select * from b inner join a_b on b.id = a_b.id where a_b.a_tag = a.id;
这样问题就出现了,我们实际上想要的sql语句为:
select * from b inner join a_b on b.id = a_b.id where a_b.a_tag = a.tag;
但是,我们应该怎么做才可以得到这样的结果呢?
那就需要看下关于belongsToMany()的第四、第五个参数了;
我先贴上laravel源码:
上面是源码,我们主要看public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null) { // If no relationship name was passed, we will pull backtraces to get the // name of the calling function. We will use that function name as the // title of this relation since that is a great convention to apply. if (is_null($relation)) { $relation = $this->guessBelongsToManyRelation(); } // First, we'll need to determine the foreign key and "other key" for the // relationship. Once we have determined the keys we'll make the query // instances as well as the relationship instances we need for this. $instance = $this->newRelatedInstance($related); $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); // If no table name was provided, we can guess it by concatenating the two // models using underscores in alphabetical order. The two model names // are transformed to snake case from their default CamelCase also. if (is_null($table)) { $table = $this->joiningTable($related); } return $this->newBelongsToMany( $instance->newQuery(), $this, $table, $foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(), $relatedKey ?: $instance->getKeyName(), $relation );
return $this->newBelongsToMany( $instance->newQuery(), $this, $table, $foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(), $relatedKey ?: $instance->getKeyName(), $relation );
我们看了上面的代码后明确知道了,belongsToMany()有7个参数,我们通过官方文档已经知道了前四个参数的作用,剩下两个参数只能我们自己通过源码来研究
$instance->newQuery(),$this, $table, $foreignPivotKey
返回值中的这几个参数我就不多做描述了,我主要描述:
$relatedPivotKey, $parentKey ?: $this->getKeyName(), $relatedKey ?: $instance->getKeyName()
这两个,也就是belongsToMany的第5、6个参数
第五个参数,我看了他的主要功能为判断是否传入该参数如果没有传入这个参数就去调用getKeyName(); 有的人要问了getKeyName()是干什么的呢?我也想问,然后我就继续查看了geiKeyName()的源码。这个方法是Model类的一个方法,用来获取表主键的.这仅仅说了getKeyName()方法,不是还有个$this没有说它到底指向的谁?它指向的是当前调用belongsToMany()的模型。
第六个参数与第五个参数作用是一样的,只是获取的模型不同而已。这个参数获取的是关联表的主键.
参数讲完了,我们传入后会得到什么样的sql呢?
belongsToMany(b:class, 'a_b', 'a_tag', 'b_id', 'tag', 'id');
下面我们就来看看到底会得到怎样的sql:
select * from b inner join a_b on b.id = a_b.id where a_b.a_tag = a.tag ; 这就是我们得到的sql;
仔细看看,是不是和我们上面想要得到的sql语句一样?
-
mysql 根据一个表的字段值不同关联查询两张不同的表
2021-01-18 21:58:37有一下三张表A,B,C,A.type=0时,target_id为B.id;A.type=1时,target_id为C.idA表idtypetarget_id111201312402_________B表idb_name1b_name12...有一下三张表A,B,C,A.type=0时,target_id为B.id;A.type=1时,target_id为C.id
A表
id type target_id
1 1 1
2 0 1
3 1 2
4 0 2
_________
B表
id b_name
1 b_name1
2 b_name2
_____________
C表
id c_name
1 c_name1
2 c_name2
_________
需求是:查询条件是name,查出B.b_name 或C.c_name like ‘%name%’ 的记录,结果字段是A.id,name
例如以上表,假如查询name1 ,结果如下
A.id name
1 c_name1
2 b_name1
问一下高手们,上面的查询语句该怎么样让写呢?在线等
解决方案
20
有一下三张表A,B,C,A.type=0时,target_id为B.id;A.type=1时,target_id为C.id
A表
id type target_id
1 1 1
2 0 1
3 1 2
4 0 2
_________
B表
id b_name
1 b_name1
2 b_name2
_____________
C表
id c_name
1 c_name1
2 c_name2
_________
本人只是随便写写,更好的 一定在其他人 那里
select a.id,b.name from a join b on a.target_id=b.id where a.type=0
union
select a.id,c.name from a join c on a.target_id=c.id where a.type=1
不太明白lz 这个逻辑是用在什么地方, sql 实现时没有问题,总感觉 这个要是太多,sql的效率比较低
20
select a.id,(CASE WHEN a.type = 0 THEN b.name else c.name END) from a
left join b on a.target_id=b.id
left join c on a.target_id=c.id
试试速度,也可以吧case when 用在join 的时候拼接sql
CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明mysql 根据一个表的字段值不同关联查询两张不同的表!
-
mysql一张表多个字段关联另一张表查询
2021-01-18 21:58:36如下:一张订单表多个字段关联用户表:1.链表查询SELECT cu.id AS 'id',cu.version AS 'version',cu.cid AS 'cid',cu.uid AS 'uid',cu.shopName AS 'shopName',cu.address ...如下:一张订单表多个字段关联用户表:
1.链表查询
SELECT cu.id AS 'id',
cu.version AS 'version',
cu.cid AS 'cid',
cu.uid AS 'uid',
cu.shopName AS 'shopName',
cu.address AS 'address',
cu.totalPrice AS 'totalPrice',
cu.orderType AS 'orderType',
cu.state AS 'state',
cu.cCreateTime AS 'cCreateTime',
cu.decorate AS 'decorate',
cu.area AS 'area',
cu.roomArea AS 'roomArea',
cu.machinePrice AS 'machinePrice',
cu.caid AS 'caid',
cu.cooperationtypeid AS 'cooperationtypeid',
cu.cooperationRebate AS 'cooperationRebate',
cu.cooperationPrcie AS 'cooperationPrcie',
cu.machineDiscount AS 'machineDiscount',
cu.alreadyPaid AS 'alreadyPaid',
cu.updateTime AS 'updateTime',
cu.cooperationdeposit AS 'cooperationdeposit',
cu.updateUserid AS 'updateUserid',
cu.overseerId AS 'overseerId',
cu.decorationQuotation AS 'decorationQuotation',
cu.machineDeposit AS 'machineDeposit',
us1.uName AS 'serviceuName',
us2.uName AS 'updateName'
FROM customerorder AS cu
LEFT JOIN userdetail AS us1
ON us1.id = cu.uid
LEFT JOIN userdetail AS us2
ON us2.id = cu.updateUserid;
2.子查询
SELECT cu.id AS 'id',
cu.version AS'version',
cu.cid AS'cid',
cu.uid AS'uid',
cu.shopName AS'shopName',
cu.address AS'address',
cu.totalPrice AS'totalPrice',
cu.orderType AS'orderType',
cu.state AS'state',
cu.cCreateTime AS'cCreateTime',
cu.decorate AS'decorate',
cu.area AS'area',
cu.roomArea AS'roomArea',
cu.machinePrice AS'machinePrice',
cu.caid AS'caid',
cu.cooperationtypeid AS'cooperationtypeid',
cu.cooperationRebate AS'cooperationRebate',
cu.cooperationPrcie AS'cooperationPrcie',
cu.machineDiscount AS'machineDiscount',
cu.alreadyPaid AS'alreadyPaid',
cu.updateTime AS'updateTime',
cu.cooperationdeposit AS'cooperationdeposit',
cu.updateUserid AS'updateUserid',
cu.overseerId AS'overseerId',
cu.decorationQuotation AS'decorationQuotation',
cu.machineDeposit AS'machineDeposit',
(SELECT uName FROM userdetail WHERE id= cu.uid) AS 'serviceuName',
(SELECT uName FROM userdetail WHERE id= cu.updateUserid) AS 'updateName'FROM customerorder AS cu;
总结:
1,表关联的效率要高于子查询,因为子查询走的是笛卡尔积
2,表关联可能有多条记录,子查询只有一条记录,如果需要唯一的列,最好走子查询
-
多表联查,两表之间相互关联,不一定需要有主外键关系,字段匹配就行
2017-05-18 17:53:30Set @dlr = 15001; SELECT school.name AS schoolName, xq.xqmc AS xqmc, qy.mc as qymc, ly.bz as lymc, fj.bz as fjmc FROM t_xsgy_ssap ssap LEFT JOIN t_xsgy_cwgl cwgl ON cwgl.id=ssap.cw -
两个没有关联字段的表,怎么按条件匹配
2018-05-23 01:28:22问题是这样的,一张职位表和一张简历表,两个没有可关联字段,按照简历表中个人的行业类型、工作类型、城市和薪资关联职位表,取职位表中与简历表四项信息吻合(行业类型相同,工作类型相同,城市相同,薪资大于职位... -
一个hive遇到的问题(两表关联,两表中关联字段唯一,关联结果表中不唯一)
2019-05-22 14:46:45记录一个hive中遇到的很有意思的问题,首先我建了两张临时表,最终要把两张表关联起来的时候,发现:两张表中的关联字段都是唯一的,关联之后关联字段竟然不唯一了!关联方法用的left join。检验sql如下: SELECT ... -
mysql实现一张表的两个字段与另一张表一个字段创建关联
2019-11-19 11:48:251.表accounts id accounts 100 主营业务收入 1001 运输收入 1002 搬运装车收入 200 主营业务成本 2001 运输成本 2.表data id pid amount 1001 100 635... -
Mysql外键关联【一张表两个字段分别与另一张表主键关联】
2019-12-05 16:00:19例如用户表 user_id user_name 1 zhangsan ... 另一张money表,表示了借钱的关系 id from to how 1 1 2 100 2 3 4 100 关联查询 select m.id,u1.use... -
MySQL - 数据库设计(表之间的 3 种关联关系)
2018-12-21 20:50:45表与表之间一般存在三种关系,即一对一,一对多,多对多关系。 下面分别就三种关系讲解数据库相关设计的思路和思考过程。 一、一对一关系 例如,下面的一张表,保存了人的相关信息,有男有女,要求查处所有的... -
mysql---多表关联
2021-01-18 22:14:00无序性:指集合内部元素没有相对顺序的概念,对于两个集合而言,仅仅要元素值和元素个数同样则两个集合相等。唯一性:指集合内部元素不存在值相等的元素。上图所看到的集合是错误的。由于有2个‘3’违背了唯一性上图... -
SQL关联同一个表中的两个字段,应该怎样关联?
2017-09-12 11:29:36比如: 人员表中,有两个分类字段(人员分类,二级分类),都要去关联 分类表,这时候应该怎么关联 我只知道一种解决方案,就起起别名,关联两遍,还有其他解决方案吗? ``` select t.name, c1.className,c2.... -
sql:一个表的两个字段关联另一个表的一个字段
2018-09-28 11:12:35select * from A a left join B b1 on a.bj_type=b1.bjType_id left join B b2 on a.dq_type=b2.bjType_id -
【SQL Join】两个字段关联到同一个表,如何使用left join
2019-09-04 17:16:05有时为了得到完整的结果,我们需要从两个或更多的表中获取结果。我们就需要执行 join。 下面列出了您可以使用的 JOIN 类型,以及它们之间的差异。 JOIN: 如果表中有至少一个匹配,则返回行 LEFT JOIN: 即使右表中... -
sql查询两个表的关联关系
2018-07-16 16:21:44在进行查询操作时,我们通常需要查询两个关联表的数据,我们可以使用where语句进行查询,如: select Emp.E_Id,Company.C_OraName from Emp,Company where Companey.C_Id=Emp.C_Id 但是我们往往会碰到比较复杂... -
sql server 两个表之间的关联(触发器)
2018-01-11 19:05:41create trigger tri_user_book on user_book after update as begin update book_borrow set book_borrow=user_book.borrow_is ...inner join inserted user_book on book_borrow.book_borrow= -
SQL 如何从两个表提取字段,合成一个表【如图】
2018-06-04 07:12:29!... 上面的两张表中拥有大量的数据 现在想建立一张中间表用sql 应该怎么处理 呢?...上面的两个表的联系就在 第二列和第三列中 比如 第一个表的张三 有语文数学英语,第二个表的语文数学英语里有张三 -
SQL数据表两个字段关联同一张数据表
2015-01-09 22:53:35SQL数据表两个字段关联同一张数据表 -
sql进行两个关联表,根据其中一个表的一个属性进行条件查询查询
2021-09-14 09:12:48这篇文章技术指导为sql进行两个关联表,根据其中一个表的一个属性进行条件查询查询 假设只有两张表,其中一张表最后一个外键连接到另一张表的主键,那我想通过其中一个属性,进行全部查询或者是条件查询 第一章表 ... -
MySQL--如何通过关联字段同步A、B两张表的字段内容
2018-01-09 10:31:52关联字段为两张表的各自的id,同步B表的b、c、d字段内容到A表的b、c、d字段内容。PS:把一张表中的某个字段内容同步到另一张表的字段,前提条件是两张表要有关联字段。 二、解决方法 2.1 建表 create table A( ... -
如何创建数据库表之间的关联关系
2015-10-24 16:04:22 一对一关联 1.一对一的单向主键关联: ...此时在两个实体类中不用添加任何的对象属性 2.一对一的单向外键关联 例如两张表:Husband和wife ***在设计实体类时建立联系: 对象 -
使用SQL 模糊查询两表关联字段数据
2020-01-09 16:45:50使用SQL 模糊查询两表关联字段数据,主要难点就是 无法直接使用like来进行数据处理, 这里使用concat来 和join on 来拼凑 模糊查询的样式 select x.medianame from tb_totle x join tb_new y on x.medianame ... -
两张表进行字段模糊匹配关联
2018-06-07 15:22:00tableauexcel -
[Mysql] 两张表关联字段同步字段内容
2019-05-16 15:01:07把一张表中的某个字段内容同步到另一张表的字段,前提条件是两张表要有关联字段。 update table_name1 a set a.name= (select b.name from table_name2 b where a.oid= b.id) where exists (select 1 from table_... -
MySQL 两张表关联,以另一个表的值更新字段值
2019-08-10 14:11:45表A关联表B,以ID字段左关联匹配、更新表A的字段SCENIC。 UPDATE A LEFT JOIN B ON A.ID=B.ID SET A.SCENIC_TYPE= B.TYPE ; -
Oracle关联两张表批量修改某个字段
2020-07-12 12:56:33有两张表A、B,需要关联这两张表,对表A中的某个字段,批量进行更新。 一、业务描述: 现有表A、B分别存储用户基本信息,且有主键可以关联。因为数据新旧等关系,表A、B中的数据不一致(表A,标红的内容),比如... -
oracle 两表关联 update的两个方法
2021-05-27 10:48:36@[TOC]oracle 两表关联 update的两个方法 oracle 两表关联 update的两个方法 条件:有表1和表2,表1有字段A,字段B,A为主键,表2也有字段A(不是主键),还有字段C,我希望把表1的字段B赋给表2的C字段。 两种方法: – ... -
mysql left join on使用两个或多个字段关联查询
2021-10-28 10:13:40mysql left join on使用两个或多个字段关联查询解决查询如下 解决 left join on 通过两个或多个字段才可以关联到一条数据 ON concat( o.to_id, o.from_id )= concat( r.to_id, r.from_id ) 查询如下 SELECT o.id,r.... -
SQL多字段JOIN(两个表JOIN ON多字段)
2021-01-12 14:02:20全网首发SQL多字段JOIN JOIN ON 多字段 HIVE多字段JOIN MySQL多字段JOIN 两个表JOIN ON多字段 JOIN ON 后面不是 等于号