“字段列表”中的未知列“ ID”,但列表中的列IS

时间:2019-02-06 12:18:00

标签: sql syntax mariadb

错误:

  

“字段列表”中的未知列“ ID”

我感到困惑。如下所示,ID wp_posts表下的一列:

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
);

查询产生错误:

DELETE FROM wp_posts 
WHERE  id IN (SELECT id 
              FROM   (SELECT id, 
                             post_title, 
                             post_type, 
                             meta_value 
                      FROM   (SELECT 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); 

) t where t.rn <> 1
) AS aliasx
);

dbfiddle here

3 个答案:

答案 0 :(得分:3)

无论数据库如何,您都具有以下代码:

                 (SELECT id, 
                         post_title, 
                         post_type, 
                         meta_value 
                  FROM   (SELECT wp_postmeta.post_id, 
                                 post_title, 
                                 post_type, 
                                 meta_value, 
                                 Row_number() 
                                   OVER( 
                                     partition BY post_title 
                                     ORDER BY wp_postmeta.meta_value) rn 

子查询中没有id。大概您打算使用post_id而不是id

答案 1 :(得分:1)

您可以像使用EXISTS

一样简化查询
DELETE wp 
FROM   wp_posts wp 
WHERE  EXISTS (SELECT 1 
               FROM   wp_postmeta md 
               WHERE  md.post_id = wp.id 
                      AND wp.post_type = 'Product' 
                      AND md.meta_key = '_regular_price') 

答案 2 :(得分:0)

Gordon Linoff是正确的(-我会接受他的回答),我不小心从子查询中省略了wp_posts.ID。解决方案是将wp_posts.ID,添加到较低顺序子查询的SELECT列表中,以便可以在较高顺序子查询中选择它:

(SELECT wp_posts.ID, wp_postmeta.post_id,post_title,post_type ,meta_value,