是否可以在 select 语句中添加通配符
Is it possible to have a where with a select statement which you add wildcards to
我有这样一种情况,我需要在 where 中使用 select 语句,然后还要将通配符追加到 return 的值。例如:
select * from [Customers.customervisibility] where userId like '%,' (Select Id from [Users.Users] where name ='MyName') ',%'
但是 运行 这给出了:
Incorrect syntax near ',%'.
现在 Select
语句只会转到 return 1 个 id,所以我不知道是否有更好的方法来编写它,也许是使用函数。
总体目标是 select 来自 [customer.customervisibility]
的行,其中 id 包含在逗号分隔的字符串列中 [UserId]
例如如果 id = 8
我需要获取 *,8,*...
所在的行
它必须是内联的,我不能使用变量,你将不得不原谅可怕的数据库设计。这样它就可以与第三方软件一起使用
试试这个:
select * from [Customers.customervisibility] where convert(VARCHAR(100),userId) like '%'+(Select Id from [Users.Users] where name ='MyName')+'%'
试试这个 where
子句
如果您的 DBMS 支持 Concat
使用这个。
userId like concat('%' ,(Select top 1 cast(Id as varchar(50)) from [Users.Users] where name ='MyName') ,'%')
其他
userId like '%' +(Select top 1 cast(Id as varchar(50)) from [Users.Users] where name ='MyName') +'%'
我已经使用 Top 1
来避免子查询 returns 超过一行的错误,如果你的子查询 returns 超过一行
SELECT *
FROM [Customers.customervisibility]
WHERE (userId LIKE '% , Select Id from [Users.Users] where status="MyName" %')
您似乎忘记了字符串的连接字符 +
。尝试以下查询:
select * from [Customers.customervisibility] where userId like '%,'+(Select Id from [Users.Users] where name ='MyName')+',%'
这将给出 userId
包含您的 ID 的结果,其中 userId
是逗号分隔的字符串。
我有这样一种情况,我需要在 where 中使用 select 语句,然后还要将通配符追加到 return 的值。例如:
select * from [Customers.customervisibility] where userId like '%,' (Select Id from [Users.Users] where name ='MyName') ',%'
但是 运行 这给出了:
Incorrect syntax near ',%'.
现在 Select
语句只会转到 return 1 个 id,所以我不知道是否有更好的方法来编写它,也许是使用函数。
总体目标是 select 来自 [customer.customervisibility]
的行,其中 id 包含在逗号分隔的字符串列中 [UserId]
例如如果 id = 8
我需要获取 *,8,*...
它必须是内联的,我不能使用变量,你将不得不原谅可怕的数据库设计。这样它就可以与第三方软件一起使用
试试这个:
select * from [Customers.customervisibility] where convert(VARCHAR(100),userId) like '%'+(Select Id from [Users.Users] where name ='MyName')+'%'
试试这个 where
子句
如果您的 DBMS 支持 Concat
使用这个。
userId like concat('%' ,(Select top 1 cast(Id as varchar(50)) from [Users.Users] where name ='MyName') ,'%')
其他
userId like '%' +(Select top 1 cast(Id as varchar(50)) from [Users.Users] where name ='MyName') +'%'
我已经使用 Top 1
来避免子查询 returns 超过一行的错误,如果你的子查询 returns 超过一行
SELECT *
FROM [Customers.customervisibility]
WHERE (userId LIKE '% , Select Id from [Users.Users] where status="MyName" %')
您似乎忘记了字符串的连接字符 +
。尝试以下查询:
select * from [Customers.customervisibility] where userId like '%,'+(Select Id from [Users.Users] where name ='MyName')+',%'
这将给出 userId
包含您的 ID 的结果,其中 userId
是逗号分隔的字符串。