MariaDB 10.1中DELETE的语法错误的未知原因

时间:2019-02-06 18:18:44

标签: mysql sql mariadb

on db Fiddle下面的代码在MariaDB 10.1上运行时提供:

错误:

  

您的SQL语法有错误;检查手册   对应于您的MariaDB服务器版本,以使用正确的语法   在'DELETE pm FROM wp_postmeta pm WHERE post_id IN(SELECT   post_id           '在第26行

查询:

-- Block 1: Deleting all duplicate products in wp_posts table 
DELETE FROM wp_posts 
WHERE  id IN (SELECT id 
              FROM   (SELECT id, 
                             post_title, 
                             post_type, 
                             meta_value 
                      FROM   (SELECT wp_posts.id, 
                                     wp_postmeta.post_id, 
                                     post_title, 
                                     post_type, 
                                     meta_value, 
                                     Row_number() 
                                       OVER( 
                                         partition BY post_title 
                                         ORDER BY wp_postmeta.meta_value) rn 
                              FROM   wp_postmeta 
                                     JOIN wp_posts 
                                       ON wp_postmeta.post_id = wp_posts.id 
                              WHERE  wp_posts.post_type = 'Product' 
                                     AND wp_postmeta.meta_key = '_regular_price' 
                             ) t 
                      WHERE  t.rn <> 1) AS aliasx); 

-- Block 2: Deleting all corresponding wp_postmeta.post_ids that don't have a match in wp_posts.id after the duplicate deletion above
DELETE pm 
FROM   wp_postmeta pm 
WHERE  post_id IN (SELECT post_id 
                   FROM  (SELECT post_id 
                          FROM   wp_postmeta pm 
                                 LEFT JOIN wp_posts p 
                                        ON p.id = pm.post_id 
                          WHERE  p.id IS NULL 
                                 AND p.post_type = "product") i) 

如何纠正此错误?我注意到,单独输入第一和第二个代码块不会给出错误,但是一起输入时会出现此错误...

2 个答案:

答案 0 :(得分:3)

WordPress使用的MySQL API不允许您提供两个串联的查询(在您的情况下为两个DELETE)。 dbfiddle也没有。分别运行查询。

如果您习惯其他表服务器,这可能会造成混淆。

答案 1 :(得分:2)

实际上,它只是拆分Delete条语句即可

create table wp_posts (
  ID integer primary key auto_increment,
  post_title varchar(30),
  post_type varchar(30)
);
create table wp_postmeta (
  ID integer primary key auto_increment,
  post_id integer,
  meta_key varchar(30) not null default '_regular_price',
  meta_value integer not null
);
insert into wp_posts (post_title, post_type) values
('Apple Pie','Product'),
('French Toast','Product'),
('Shepards Pie','Product'),
('Jam Pie','Product'),
('Jam Pie','Product'),
('Plate','Not a Product'),
('Bucket','Not a Product'),
('Chequebook','Not a Product'),
('French Toast','Product'),
('French Toast','Product'),
('Banana','Product'),
('Banana','Product'),
('Banana','Product');
insert into wp_postmeta (post_id, meta_value) values
(1,10),
(2,5),
(3,9),
(4,8),
(5,11),
(6,12),
(7,10),
(8,6),
(9,1),
(10,1),
(11,7),
(12,2),
(13,2);
-- Deleting all duplicate products in wp_posts table 
DELETE FROM wp_posts 
WHERE  id IN (SELECT id 
              FROM   (SELECT id, 
                             post_title, 
                             post_type, 
                             meta_value 
                      FROM   (SELECT wp_posts.id, 
                                     wp_postmeta.post_id, 
                                     post_title, 
                                     post_type, 
                                     meta_value, 
                                     Row_number() 
                                       OVER( 
                                         partition BY post_title 
                                         ORDER BY wp_postmeta.meta_value) rn 
                              FROM   wp_postmeta 
                                     JOIN wp_posts 
                                       ON wp_postmeta.post_id = wp_posts.id 
                              WHERE  wp_posts.post_type = 'Product' 
                                     AND wp_postmeta.meta_key = '_regular_price' 
                             ) t 
                      WHERE  t.rn <> 1) AS aliasx); 
-- Deleting all corresponding wp_postmeta.post_ids that don't have a match in wp_posts.id after the duplicate deletion above
DELETE pm 
FROM   wp_postmeta pm 
WHERE  post_id IN (SELECT post_id 
                   FROM  (SELECT post_id 
                          FROM   wp_postmeta pm 
                                 LEFT JOIN wp_posts p 
                                        ON p.id = pm.post_id 
                          WHERE  p.id IS NULL 
                                 AND p.post_type = "product") i) 

db <>提琴here