准备好的语句和 IN 表达式
我有一个数据库,用户可以在其中搜索包含一个或多个项目列表的记录。我正在使用 IN 进行搜索,但无法让 IN 使用准备好的语句。这是我尝试过的:
SELECT * FROM tbl1 WHERE col IN (?)
但是准备好的语句将我传递给它的项目列表视为单个项目。我怎样才能做到这一点?
我正在使用 sqlite,如果它有什么区别的话。
I have a database where users can search for records that have one or more of a list of items. I'm using IN to do the search, but I can't get IN to work with prepared statements. This is what I've tried:
SELECT * FROM tbl1 WHERE col IN (?)
But the prepared statement treats the list of items I pass it as a single item. How can I make this work?
I'm using sqlite, if it makes any difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能这样做,因为你不能绑定到数组。
您必须分两步完成:
无论数据库如何,都是如此。
您没有说子 SELECT 是否是更好的解决方案,但如果有问题的值在另一个表中可用,也许它可以工作。
You can't do IN this way, because you can't bind to an array.
You have to do it in two steps:
This is true regardless of database.
You don't say whether a sub-SELECT could be a better solution, but perhaps it could be made to work if the values in question were available in another table.
您可以使用临时表和子查询:
You can use a temp table and subquery: