简体   繁体   English

Mysql将表1中的同一列作为外键引用到表2中的两列

[英]Mysql referencing the same column from table 1 as foreign key to two columns in table two

I have two mysql tables with the second one having foreign key references to the first one. 我有两个mysql表,第二个具有对第一个的外键引用。 The first table is as follows: 第一个表如下:

1st Table name: treeview 第一个表名称:treeview

+----+-----------+-----------+-----------+
| id | name      | text      | parent_id |
+----+-----------+-----------+-----------+
|  1 | BrandTree | BrandTree | 0         |
|  2 | TShirt    | TShirt    | 1         |
|  3 | ManForce  | ManForce  | 2         |
|  4 | PayTM     | PayTM     | 2         |
+----+-----------+-----------+-----------+

I am using this table to generate a jstree. 我正在使用此表生成jstree。 And my second table is as follows: 我的第二张表如下:

2nd table name: annotations: 第2个表格名称:注释:

+--------+-------------------------------------+--------------+-----------+
| ann_id | imagename                           | locationName | brandname |
+--------+-------------------------------------+--------------+-----------+
|      1 | 95-20180527-190018-205342-00002.jpg |            2 |         3 |
|      2 | 95-20180527-190018-205342-00005.jpg |            2 |         4 |
+--------+-------------------------------------+--------------+-----------+

In the second table, the locationName and brandname have foreign key references to first table id column. 在第二个表中,locationName和brandname具有对第一个表id列的外键引用。 I am using the following code to retrieve the table: 我正在使用以下代码来检索表:

select annotations.imagename, treeview.name, treeview.text 
from annotations 
inner join treeview on treeview.id = annotations.locationName 
and inner join treeview on treeview.id = annotations.brandname;

The above code provides a Empty set. 上面的代码提供了一个空集。

Can I have the id column from table 1 as foreign key for two columns in table 2? 我可以将表1中的id列用作表2中两列的外键吗? How do i fetch in this case? 在这种情况下,我该如何取货?

That's doing one join, twice 一次加入,两次

select annotations.imagename, tv1.name, tv2.name 
from annotations 
inner join treeview tv1 on tv1.id = annotations.locationName 
inner join treeview tv2 on tv2.id = annotations.brandname;

I think, haven't got mysql handy 我认为,没有方便的mysql

You should use two join with thetreeview table one for each column that refer to the table annotations 您应该对树视图表使用两个联接,每一列引用表注释

    select annotations.imagename, tv1.name, tv1.text, t2.name, tv2.text
    from annotations 
    inner join treeview tv1 on tv1.id = annotations.locationName 
    inner join treeview tv2  on tv2.id = annotations.brandname;

You have to use different alias names for treeview tables that you have used twice. 您必须为两次使用的treeview表使用不同的别名。 And not required and before inner join... . 并且不需要andinner join...之前inner join...

Query 询问

select `t1`.`imagename`, `t2`.`name`, `t3`.`text`
from `annotations` as `t1` 
inner join `treeview` as `t2`
on `t2`.`id` = `t1`.`locationName`
inner join `treeview` as `t3` 
on `t3`.`id` = `t1`.`brandname`;

Find a demo here 在此处找到演示

从注释a,树视图t1,树视图t2中选择一个图像名称,t1名称,t2名称,其中a.locationName = t1.id && a.brandname = t2.id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM