SELECT * FROM users WHERE uid IN (SELECT doctors FROM MainPage WHERE Valid=1)
users
表格uid INT
Mainpage
表doctors 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中找到一个等价物来实现相同的结果?
答案 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),
','))