如何使用引用多个表的WHERE子句删除sqlite中的行?

时间:2019-05-16 00:02:53

标签: c sqlite

给出下表:

acl (acl_id integer, name character)
ace (acl_id integer, users_id integer)
users (users_id integer, login character)

以及一些示例数据:

acl (0, 'view') (1, 'sell'), (2, 'void')
ace (0, 9), (1, 9), (2, 9), (0, 8)
users (8, 'joe') (9, 'john')

我尝试了以下操作:

DELETE FROM ace
  WHERE ace.acl_id = acl.acl_id AND acl.name = ?
    AND ace.users_id = users.users_id AND users.login = ?

但这在sqlite3_prepare_v2()中失败,并显示SQLITE_ERROR“没有这样的列:acl.acl_id” (不是很有帮助)

我得到相同的结果:

DELETE FROM ace
  WHERE acl_id = acl.acl_id AND acl.name = ?
    AND users_id = users.users_id AND users.login = ?

非常感谢,吓人:

DELETE FROM ace,acl,users
  WHERE ace.acl_id = acl.acl_id AND acl.name = ?
    AND ace.users_id = users.users_id AND users.login = ?

在第一个逗号处出现语法错误。

如果我有足够的能力将值'sell'和'john'绑定到参数,并执行查询,那么我希望ace表的(1,9)行将被删除。

是否可以先不执行两个SELECT以获得acl_id和users_id值来完成?

1 个答案:

答案 0 :(得分:1)

在评论中链接的Q / A的帮助下,我得以提出一个功能查询:

DELETE FROM ace
 WHERE acl_id = (
       SELECT acl_id
         FROM acl
        WHERE name = ?
   )
   AND users_id = (
       SELECT users_id
         FROM users
        WHERE login = ?
   )

我不得不说,这太荒谬了。

“ SQL,查询语言的EBCDIC”