【问题标题】:Use a table in Where clause在 Where 子句中使用表
【发布时间】:2012-05-20 12:21:39
【问题描述】:

我的数组包含一些元素:

(
    "element 1",
    "element 2",
    "element 3"
)

我需要查询数据库,并获取存储在数据库中上述数组中的每个元素:

 select * from table1 where type=?

我想到了for循环,对于数组,select查询会根据数组的大小重复,用处不大。

任何想法,提前谢谢:)

【问题讨论】:

    标签: sql ios sqlite


    【解决方案1】:

    使用IN operator

    select * from table1 where type in ('element1', 'element2');
    

    【讨论】:

    • 嗨,谢谢您的回复,但在我的情况下,'element1', 'element2' 存储在NSMutableArray 中,那么如何将NSMutable 数组转换为这样的字符串?
    • 您应该使用参数化查询:select * from table where type in (?, ?) 然后bind them to your values
    【解决方案2】:

    你的意思是像

        select * from table1 where type in ('type1', 'type2', 'type3')
    

    ?

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2011-09-19
      • 1970-01-01
      相关资源
      最近更新 更多