SQL Server中的FIND_IN_SET()等价物

时间:2017-01-12 17:18:36

标签: sql-server tsql

SELECT * FROM users WHERE uid IN (SELECT doctors FROM MainPage WHERE Valid=1)
  

users表格uid INT   
Mainpagedoctors text,其值为1,2,3,4,5

当我运行上面的查询时,它只产生1行,这是uid = 1,但我想要所有5行。所以在MySQL中我使用了这个查询:

SELECT *
FROM users
JOIN MainPage ON FIND_IN_SET(uid, doctors)
WHERE Valid = 1;

有效。我试图在{@ 1}}的SQL Server中找到一个等价物来实现相同的结果?

2 个答案:

答案 0 :(得分:5)

这可能有效

SELECT *
FROM users u
WHERE EXISTS (
    SELECT *
    FROM MainPage
    WHERE CONCAT(',',doctors,',') LIKE CONCAT('%,',u.uid,',%')
        AND Valid = 1
)

答案 1 :(得分:0)

自SQL Server 2016(13.x)起,STRING_SPLIT()

SELECT *
FROM users
WHERE uid IN
 (SELECT value FROM STRING_SPLIT(
     (SELECT doctors FROM MainPage WHERE Valid = 1),
     ','))