【发布时间】:2015-08-05 07:51:39
【问题描述】:
我有一种情况,我需要在 where 中使用 select 语句,然后还要在它返回的值上附加和前置通配符。例如:
select * from [Customers.customervisibility] where userId like '%,' (Select Id from [Users.Users] where name ='MyName') ',%'
但运行它会给出:
',%' 附近的语法不正确。
现在Select 语句只会返回 1 个 id,所以我不知道是否有更好的方法来编写它可能使用函数。
总体目标是让我可以从[customer.customervisibility] 中选择id 包含在逗号分隔的字符串列[UserId] 中的行
例如如果id = 8
我需要获取*,8,*...所在的行
它必须是内联的,我不能使用变量,你将不得不原谅糟糕的数据库设计。这是为了它可以与第三方软件一起使用
【问题讨论】:
-
您不需要连接字符串吗?
where userId like '%,'+ (select....) + ',%' -
如果我这样做,我会得到:
Conversion failed when converting the varchar value '%,' to data type int.并且UserId列是nvarchar(max) -
你用的是什么关系型数据库?
-
Microsoft Sql Server.. 实际上它的 Azure Sql,但相同的区别
-
USE convert(VARCHAR(100),userId) like '%'+(Select Id from [Users.Users] where name ='MyName')+'%'