Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql In子句将字符串值传递给整型列时出错_Sql_Sql Server 2012 - Fatal编程技术网

Sql In子句将字符串值传递给整型列时出错

Sql In子句将字符串值传递给整型列时出错,sql,sql-server-2012,Sql,Sql Server 2012,我有如下疑问 select * from table1 where id in (select ids from table2 where id = 100) 如果我单独运行子查询,它将返回类似34,35,55,66的输出 但我上面的查询给出的错误如下 Conversion failed when converting the varchar value '34,35,55,56' to data type int. 表1中的id列是int。我知道我可以在两个单独的查询中执行此操作,但如果有

我有如下疑问

select * from table1 where id in (select ids from table2 where id = 100)
如果我单独运行子查询,它将返回类似34,35,55,66的输出

但我上面的查询给出的错误如下

Conversion failed when converting the varchar value '34,35,55,56' to data type int.
表1中的id列是int。我知道我可以在两个单独的查询中执行此操作,但如果有任何方法可以在单个查询中执行,请让我知道。
提前非常感谢

我不知道有关sql server的情况,而且关于这种结构的评论是不受欢迎的,尽管如此,您可以在mysql中使用以下内容来实现,per

从表1中选择不同的id,并在集合中查找表2(id,id)

更新:我刚刚意识到这并不反映
table2
有自己的
id
字段并基于此限制搜索。如果您需要帮助修改查询以适应,请告诉我,我会更新它和小提琴

更新2:这里有一个蹩脚但可能有效的尝试,可以在SQL Server中使用更有限的字符串工具来实现这一点。如果有人知道SQL Server的更简单的
find_in_set
等价物,我很想知道它是什么

select distinct id from table1 join table2 
  on patindex('%,'+cast(id as varchar)+',%',ids)>0 or
  patindex(cast(id as varchar)+',%',ids)>0 or
  patindex('%,'+cast(id as varchar),ids)>0 or
  patindex(cast(id as varchar),ids)>0

请不要在一列中存储多个值。对你来说,这真是一种痛苦。你真的应该改变你的数据库结构。@juergend不,我不能改变它现有的数据库。即使是现有的数据库也可以改变。。。即使您必须更改一些内容,也请考虑这样做。碰巧包含多组数字和逗号的单个字符串与多个整数之间存在逻辑差异。这在SQL中是正确的,就像在我能想到的任何其他语言中一样。更改结构确实会更好,因为使用当前的设计,您将继续遇到难点。如果你坚持要继续,你需要在逗号上查找拆分字符串-这在这里已经被询问和回答了很多次了。@Damien_不信者,你能给我一个链接,我们可以在逗号中拆分字符串吗?看起来你可以用它来代替
find_in_set
你能把它转换成sql吗?我给你+1以感谢你的努力。但是现在就别管它了,因为我认为用一个查询是不可能的。我放弃:(@AshReva:16小时前我对答案所做的更新在SQL Server中通过我包含的fiddle链接工作。你是在寻找其他版本的SQL吗?