我无法理解如何从与另一个表数据相比较的表中获取唯一的列(删除重复)。
在我的情况下
我有两张桌子

我想从tblproduct中获得独特的行,在与tblviewer进行比较之后,作为[在表查看器中,先获取viewerid,然后在查看器表中的productid,在使用tblproduct后获取productid)。
实际上就像那样
如果取vieweris=123,两行productid,在此之后选择12001&11001,最后从加工的产品中取行。
select *
from tblproduct
where productid =
(
select distinct(productid)
from tblviewer
where viewerid = 123
)发布于 2016-06-22 20:49:12
有几种方法可以做到这一点。您可以对表执行一个标准的INNER JOIN来过滤结果:
Select Distinct P.*
From tblProduct P
Join tblViewer V On V.ProductId = P.ProductId
Where V.ViewerId = 123或者,您也可以使用EXISTS --这完全消除了使用DISTINCT的需要:
Select *
From tblProduct P
Where Exists
(
Select *
From tblViewer V
Where V.ProductId = P.ProductId
And V.ViewerId = 123
)或者,您也可以使用IN,正如其他答案所建议的那样:
Select *
From tblProduct
Where ProductId In
(
Select ProductId
From tblViewer
Where ViewerId = 123
)发布于 2016-06-22 20:49:10
我认为您只是想使用IN子句,不需要使用distinct
select *
from tblproduct
where productid in
(
select productid
from tblviewer
where viewerid = 123
)发布于 2016-06-22 20:51:11
我不知道你在问什么,但我想是的,
select *
from tblproduct
where productid in
(
select distinct(productid)
from tblviewer
)https://stackoverflow.com/questions/37977924
复制相似问题