0

I want to delete an entry in my PAGES table. Upon deletion of it, it will cascade to OBJECTS table. No worries in here, it's working if I delete the entry by using simple DELETE. However, I need to specify some conditions:

PAGES table

+--------------------------+--------------+------+-----+---------+----------------+
| Field                    | Type         | Null | Key | Default | Extra          |
+--------------------------+--------------+------+-----+---------+----------------+
| page_id                  | int(11)      | NO   | PRI | NULL    | auto_increment |
| users_id                 | int(11)      | NO   | MUL | NULL    |                |
| page_value               | varchar(20)  | NO   | UNI | NULL    |                |
+--------------------------+--------------+------+-----+---------+----------------+

OBJECTS table

+----------------------------+-------------+------+-----+---------+----------------+
| Field                      | Type        | Null | Key | Default | Extra          |
+----------------------------+-------------+------+-----+---------+----------------+
| objects_id                 | int(11)     | NO   | PRI | NULL    | auto_increment |
| page_id                    | int(11)     | NO   | MUL | NULL    |                |
| objects_name               | varchar(50) | NO   |     | NULL    |                |
| objects_avail              | varchar(20) | NO   |     | NULL    |                |
+----------------------------+-------------+------+-----+---------+----------------+

If objects_avail == "ALL", I must not include that entry in cascade delete. I came up with this SQL query but got an error:

$query = "
    DELETE FROM pages AS p 
    INNER JOIN objects AS o ON p.page_id = o.page_id 
    WHERE p.page_id = ? 
      AND p.users_id = ? 
      AND p.page_value = ? 
      AND o.objects_avail != ?";

The error thrown:

["42000",1064,"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS p INNER JOIN objects AS o ON p.page_id = o.page_id WHER' at line 1"]

Example value for the PDO placeholders:

$params = array(81,5,"main page","ALL");

where all of this is valid and I'm sure this is not where the problem is.

I doubt or prettry sure I'm missing some in my query, any suggestions please?

4

1 回答 1

2

对于内部连接 ​​UPDATE 或 DELETE,您需要明确指定实际要删除的表,否则解析器将不知道您的意思。您可以选择 1 个或多个要从中删除的表。在您的情况下,只删除页面的别名 p 是有意义的。

DELETE p
FROM pages AS p
INNER JOIN objects AS o ON p.page_id = o.page_id
WHERE
    p.page_id = ? AND
    p.users_id = ? AND
    p.page_value = ? AND
    o.objects_avail != ?

我唯一改变的行是 DELETE 变成了 DELETE p

于 2012-05-21T04:04:22.963 回答