“Select in” with “ordered pairs”

久未见 提交于 2019-12-13 19:02:44

问题


I have a table structure like this.

ProductCR   productID   ProductName 
09          1553        A1 
09          1600        A2 
09          1800        A3
10          1553        A4 
10          1600        A5 
10          2000        A6

I want to make something like this:

Select ProductoName from Products where (ProductCR,ProductID) in ((09,1553),(10,1600),(10,2000))

Result:
    A1
    A5
    A6

Is this posible in Sql Server?? such a "select in" with "ordered pairs"?? Thanks, Victor.


回答1:


It is not possible. I this this is a good option:

DECLARE @orderedPairs TABLE (cr int, id int)

INSERT INTO @orderedPairs (cr, id)
VALUES (09,1553),(10,1600),(10,2000)

SELECT ProductName
  FROM Products
  join @orderedPairs on ProductCR = cr
                    and ProductID = id



回答2:


Oracle allows that, but SQL Server does not. You'll have to write it out:

select  ProductoName 
from    Products 
where   ProductCR = 09 and ProductID = 1553 or
        ProductCR = 10 and ProductID = 1600 or
        ProductCR = 10 and ProductID = 2000


来源:https://stackoverflow.com/questions/7698886/select-in-with-ordered-pairs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!