如何连接两个表而不重复来自两个表的数据? [英] How to join two tables without repeating data from both the tables?
问题描述
我想加入2个表,我写了下面的查询。但是我得到了重复的记录
SELECT T.TCode,V.VehicleID
FROM TblTransactions T
JOIN TblConfirmedVehicleMap V ON V.ReplyID = T.ReplyID
WHERE T.LoadID = 1
输出如下:
TCode VehicleID
-------------------------- -----------
TN160200000011 1
TN160200000011 3
TN160200000011 4
TN160200000012 1
TN160200000012 3 >
TN160200000012 4
TN160200000013 1
TN160200000013 3
TN160200000013 4
我想输出如下:
TCode VehicleID
--------------------------- ----------
TN160200000011 1
TN160200000012 3
TN160200000013 4
TN160200000011,TN160200000012& TN160200000011是TCode,而1,2& 3是VehicleID
我尝试过:
我试过很多东西。加入,联合所有,相交,但不能得到欲望输出。
试试这个
SELECT T.TCode,(选择 distinct V.VehicleID 来自 TblConfirmedVehicleMap V 其中 V.ReplyID = T.ReplyID)
FROM TblTransactions T
WHERE T.LoadID = 1
但我担心在其中一个或两个表中都有重复的回复ID。如果是这样,你仍然希望选择其中一个回复ID,那么试试这个:
SELECT T.TCode,(选择 V.VehicleID 来自 TblConfirmedVehicleMap V 其中 V.ReplyID = T.ReplyID 和 rownum = 1 )
FROM TblTransactions T
WHERE T.LoadID = 1
希望有所帮助:)
此链接将帮助您解决查询问题: http://www.tutorialspoint.com/sql/sql-using-joins.htm
I want to join 2 tables,I wrote following query.But I'm getting duplicate records
SELECT T.TCode,V.VehicleID
FROM TblTransactions T
JOIN TblConfirmedVehicleMap V ON V.ReplyID=T.ReplyID
WHERE T.LoadID=1
I got output like:
TCode VehicleID
-------------------------- -----------
TN160200000011 1
TN160200000011 3
TN160200000011 4
TN160200000012 1
TN160200000012 3
TN160200000012 4
TN160200000013 1
TN160200000013 3
TN160200000013 4
I want output like:
TCode VehicleID
-------------------------- -----------
TN160200000011 1
TN160200000012 3
TN160200000013 4
TN160200000011,TN160200000012 & TN160200000011 are TCode,whereas 1,2 & 3 are VehicleID
What I have tried:
I have tried so many things.Joins,union all,intersect,but not able to get desire output.
Try this
SELECT T.TCode, (Select distinct V.VehicleID from TblConfirmedVehicleMap V where V.ReplyID=T.ReplyID) FROM TblTransactions T WHERE T.LoadID=1
But I fear that there are repeating Reply IDs in either or both the tables. If so and you still want one of the Reply IDs to get selected then try this:
SELECT T.TCode, (Select V.VehicleID from TblConfirmedVehicleMap V where V.ReplyID=T.ReplyID and rownum = 1) FROM TblTransactions T WHERE T.LoadID=1
Hope it helps :)
This link will help you to resolve you query: http://www.tutorialspoint.com/sql/sql-using-joins.htm
这篇关于如何连接两个表而不重复来自两个表的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!