2017-07-30 50 views
0

比方说,我有2个表 - item_imagesimages使用内部查询时mysql错误1175

当我运行查询

SELECT image_id FROM item_images WHERE item_id=1 

我得到image_id56

当我运行

DELETE FROM images WHERE id in (5, 6); 

它也可以和删除这些2行。

但是,当我试图链这两个查询在一起,它失败,错误1175

DELETE FROM images WHERE id in (SELECT image_id FROM item_images WHERE item_id=1); 

    Error Code: 
1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect. 0.000 sec 

id字段设置为私有密钥,而不是空。

为什么会发生这种情况,如果如果id在WHERE显然是私钥?

解决此问题的唯一方法是禁用安全模式,还是有其他方法?

谢谢!

+0

假设'id'列('images'表)总是大于零:'... WHERE id> 0 AND id in(SELECT image_id ...'。 – wchiquito

+0

@wchiquito,有趣 - 但我得到'(in)'中的语法错误:意外'当我尝试这样做时:S – Ward

回答

0

假设id柱(images表)总是大于零(0):

mysql> SET SESSION SQL_SAFE_UPDATES := 1; 
Query OK, 0 rows affected (0.00 sec) 

mysql> DROP TABLE IF EXISTS `item_images`, `images`; 
Query OK, 0 rows affected (0.09 sec) 

mysql> CREATE TABLE IF NOT EXISTS `images` (
    -> `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY 
    ->); 
Query OK, 0 rows affected (0.00 sec) 

mysql> CREATE TABLE IF NOT EXISTS `item_images` (
    -> `item_id` BIGINT UNSIGNED NOT NULL, 
    -> `image_id` BIGINT UNSIGNED NOT NULL 
    ->); 
Query OK, 0 rows affected (0.00 sec) 

mysql> INSERT INTO `images` 
    -> VALUES (NULL), (NULL), (NULL), 
    ->  (NULL), (NULL), (NULL); 
Query OK, 6 rows affected (0.00 sec) 
Records: 6 Duplicates: 0 Warnings: 0 

mysql> INSERT INTO `item_images` 
    -> (`item_id`, `image_id`) 
    -> VALUES (1, 5), (1, 6), (2, 1), 
    ->  (2, 3), (3, 2), (4, 2); 
Query OK, 6 rows affected (0.00 sec) 
Records: 6 Duplicates: 0 Warnings: 0 

mysql> SELECT `image_id` 
    -> FROM `item_images` 
    -> WHERE `item_id` = 1; 
+----------+ 
| image_id | 
+----------+ 
|  5 | 
|  6 | 
+----------+ 
2 rows in set (0.00 sec) 

mysql> DELETE 
    -> FROM `images` 
    -> WHERE `id` IN (SELECT `image_id` 
    ->    FROM `item_images` 
    ->    WHERE `item_id` = 1); 
ERROR 1175 (HY000): You are using safe update mode and you tried to update 
        a table without a WHERE that uses a KEY column 

mysql> DELETE 
    -> FROM `images` 
    -> WHERE `id` > 0 AND 
    ->  `id` IN (SELECT `image_id` 
    ->    FROM `item_images` 
    ->    WHERE `item_id` = 1); 
Query OK, 2 rows affected (0.01 sec) 

参见db-fiddle

UPDATE

在第一DELETE索引(key)没有达到。

mysql> SET SESSION SQL_SAFE_UPDATES := 0; 
Query OK, 0 rows affected (0.00 sec) 

mysql> EXPLAIN DELETE 
    -> FROM `images` 
    -> WHERE `id` IN (SELECT `image_id` 
    ->    FROM `item_images` 
    ->    WHERE `item_id` = 1); 
+----+--------------------+-------------+------------+------+---------------+------+---------+------+------+----------+-------------+ 
| id | select_type  | table  | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra  | 
+----+--------------------+-------------+------------+------+---------------+------+---------+------+------+----------+-------------+ 
| 1 | DELETE    | images  | NULL  | ALL | NULL   | NULL | NULL | NULL | 6 | 100.00 | Using where | 
| 2 | DEPENDENT SUBQUERY | item_images | NULL  | ALL | NULL   | NULL | NULL | NULL | 6 | 16.67 | Using where | 
+----+--------------------+-------------+------------+------+---------------+------+---------+------+------+----------+-------------+ 
2 rows in set (0.00 sec) 

mysql> EXPLAIN DELETE 
    -> FROM `images` 
    -> WHERE `id` > 0 AND 
    ->  `id` IN (SELECT `image_id` 
    ->    FROM `item_images` 
    ->    WHERE `item_id` = 1); 
+----+--------------------+-------------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+ 
| id | select_type  | table  | partitions | type | possible_keys | key  | key_len | ref | rows | filtered | Extra  | 
+----+--------------------+-------------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+ 
| 1 | DELETE    | images  | NULL  | range | PRIMARY  | PRIMARY | 8  | const | 6 | 100.00 | Using where | 
| 2 | DEPENDENT SUBQUERY | item_images | NULL  | ALL | NULL   | NULL | NULL | NULL | 6 | 16.67 | Using where | 
+----+--------------------+-------------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+ 
2 rows in set (0.01 sec) 

请参阅db-fiddle

+0

是的,它的工作原理我的错误 – Ward

+0

任何想法为什么mysql会这样操作并且只在插入id> 0时才起作用 – Ward

+0

@Ward:Updated回答。 – wchiquito