[英]Joining of two tables without any primary key both tables have foreign key
I have three tables : 我有三个表:
List(lid,list_name) 清单(lid,list_name)
Phone(pid,lid,phno) 电话(pid,lid,phno)
Emails(eid,lid,email) 电子邮件(eid,lid,email)
Tables having records as shown here 具有记录的表如下所示
lid ----------- list_name
**1** ------------- **ads**
**2** ------------- **visits**
pid ------- lid ----------- phno
10 ---------**1** ---------- 9988123456
20 ---------**1** ---------- 9988123454
30 ---------**2** ---------- 9988222222
40 ---------**2** ---------- 9988333333
eid ------- lid ----------- email
1 ---------**1** ---------- xyz@abc.com
2 ---------**1** ---------- abc@abc.com
3 ---------**2** ---------- list2@abc.com
4 ---------**2** ---------- list21@abc.com
I want to create join between Table Emails and Phone with foreign key lid. 我想在带有外键盖的表电子邮件和电话之间创建联接。
I tried it like this 我这样尝试过
SELECT * FROM phone left join emails on phone.lid = emails.lid WHERE 1
and got output form this query 并从此查询输出
pid -- lid ---- phno ---------- eid --- lid --- email
10 ----- 1 --- 9988123456 -- **1** --- 1 --- xyz@abc.com
20 ----- 1 --- 9988123454 ---**1** --- 1 --- xyz@abc.com
10 ----- 1 --- 9988123456 ---**2** --- 1 --- abc@abc.com
20 ----- 1 --- 9988123454 ---**2** --- 1 --- abc@abc.com
30 ----- 2 --- 9988222222 -- **3** --- 2 --- list2@abc.com
40 ----- 2 --- 9988333333 ---**3** --- 2 --- list2@abc.com
30 ----- 2 --- 9988222222 ---**4** --- 2 --- list21@abc.com
40 ----- 2 --- 9988333333 ---**4** --- 2 --- list21@abc.com
Here you can see we are getting duplicate emails and duplicate phone number, but I want the result that there should not be any duplicate email and duplicate phone in any row they both must be unique as shown below: 在这里您可以看到我们收到重复的电子邮件和重复的电话号码,但是我希望结果是在任何行中都不应有重复的电子邮件和重复电话,它们都必须是唯一的,如下所示:
pid -- lid ---- phno ---------- eid --- lid --- email
20 ----- 1 --- 9988123454 ---**1** --- 1 --- xyz@abc.com
10 ----- 1 --- 9988123456 ---**2** --- 1 --- abc@abc.com
30 ----- 2 --- 9988222222 -- **3** --- 2 --- list2@abc.com
40 ----- 2 --- 9988333333 ---**4** --- 2 --- list21@abc.com
I'm stuck here. 我被困在这里。 Please suggest me the solution. 请给我建议解决方案。
Your table structure would not allow you to get the output you expect. 您的表结构不允许您获得期望的输出。 In table Phone (according to given data) lid cannot be a candidate key. 在表Phone(根据给定数据)中, 盖不能用作候选键。 Same goes to lid in table Emails. 表电子邮件的盖子也是如此。
lid cannot be used as a foreign key. 盖子不能用作外键。 A foreign key must reference either the primary key or a unique key(candidate key) of the parent table. 外键必须引用父表的主键或唯一键(候选键)。 But this is not the case in either tables. 但这两个表都不是这种情况。
I would suggest introducing a new column to get the one-to-one mapping you need 我建议引入一个新列,以获取您所需的一对一映射
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.