如何在sql中重复连接三个表?
问题描述:
我有三个表...
1)t_quoteMaster如何在sql中重复连接三个表?
quoteId quoteNo quoteDate custName itemName itemQty itemPrice itemToal
10 2 17/10/17 cust1 item1 2 100 200
11 2 17/10/17 cust1 item2 5 20 100
2)t_custMaster
custId custName custAddress custGSTIN custPhone
10 cust1 US 123456789 123456789
11 cust2 UK 987654321 987654321
3)t_productMaster
productId productName productHSN productUnit productPrice
15 item1 1111111111 kg 100
16 item2 2222222222 gram 20
17 item3 3333333333 kg 50
现在我想加入的表上CUSTNAME和itemName ..
结果应该是..
quoteId quoteNo quoteDate custName itemName itemQty itemPrice itemToal productHSN productUnit custAddress custGSTIN
10 2 17/10/17 cust1 item1 2 100 200 1111111111 kg US 123456789
11 2 17/10/17 cust1 item2 5 20 100 2222222222 gram US 123456789
我需要从从选择(custAddress和custGSTIN)t_custMaster和(productHSN和productUnit)t_productMasterCUSTNAME和的基础上分别ITEMNAME从t_quoteMaster并将其附加到t_quoteMaster的末尾。
我想下面的查询,但如果在一个一对多的基础上加入时,一个常见的问题给出了行的重复..
SELECT t_quoteMaster.*,
t_productMaster.productHSN,
t_productMaster.productUnit,
t_custMaster.custAddress,
t_custMaster.custGSTIN
FROM t_quoteMaster
INNER JOIN t_productMaster
ON t_quoteMaster.itemName = t_productMaster.productName
INNER JOIN t_custMaster
ON t_quoteMaster.custName = t_custMaster.custName
where t_quoteMaster.quoteNo = '2'
答
重复。您可以使用DISTINCT
删除重复项。
SELECT DISTINCT t_quoteMaster.*,
t_productMaster.productHSN,
t_productMaster.productUnit,
t_custMaster.custAddress,
t_custMaster.custGSTIN
FROM t_quoteMaster
INNER JOIN t_productMaster
ON t_quoteMaster.itemName = t_productMaster.productName
INNER JOIN t_custMaster
ON t_quoteMaster.customerName = t_custMaster.custName
where quoteNo = '2'
+0
使用DISTINCT也不能解决它没有改变的问题.. –
+0
我接受这个答案,因为这也是解决方案,.. .. –
你能显示重复的结果吗? – Harry
这个例子不会重复... – kbball
这就是为什么我想看到OP正在谈论的重复行..我看不到它重复。 – Harry