SQLITE:INTERSECT / UNION选择结果&正常数组

时间:2016-03-23 11:33:18

标签: ios objective-c sqlite fmdb

我现在使用下面的解决方案,结果就是我现在所期望的。

a-zA-Z0-9-

但我想知道myIDArray = @[@1, @2, @3]; ... executeQuery:[NSString stringWithFormat:@"" "SELECT name" " FROM TABLE_A" " WHERE a_id IN (" " SELECT b_id" " FROM TABLE_B" " )" " AND a_id IN %@", myIDArray]; 内部INTERSECT 两个ID列表吗?像

IN (?)

当然,这不起作用,会抛出语法错误。

我还尝试使用executeQuery:[NSString stringWithFormat:@"" "SELECT name" " FROM TABLE_A" " WHERE a_id IN (" " SELECT b_id" " FROM TABLE_B" " INTERSECT" " %@"// how to put my id array here appropriately? " )", myIDArray]; 将数组转换为字符串,w /或w {o -componentsJoinedByString:作为字符串。没有运气。

1 个答案:

答案 0 :(得分:1)

INTERSECT需要查询,因此您需要一个返回所有这些值的查询:

... IN (SELECT b_id FROM TableB
        INTERSECT
        SELECT id FROM (SELECT 1 AS id UNION ALL
                        SELECT 2       UNION ALL
                        ...
                        SELECT 42));

这比使用两个IN更加冗长,效率更低。