通过子查询的别名选择列会产生错误

时间:2019-02-07 17:57:11

标签: mysql syntax mariadb

特别是,我想通过子查询的别名来选择一列,但这会产生错误。我在下面的完整查询中特别提到了这一行:

    WHERE id NOT IN (SELECT x.minid (SELECT p.post_title, MIN(m.id) as minid, m.meta_value
...) as x );
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 NOT IN (SELECT x.minid (SELECT p.post_title, MIN(m.id) as minid, m.meta_value
FROM 
    wp_postmeta m
    INNER JOIN wp_posts p 
        ON  p.id = m.post_id
        AND p.post_type = 'Product' 
WHERE 
    m.meta_key = '_regular_price'
    AND NOT EXISTS (
        SELECT 1
        FROM 
            wp_postmeta m1
            INNER JOIN wp_posts p1
                ON  p1.id = m1.post_id
                AND p1.post_type = 'Product'
        WHERE 
            m1.meta_key = '_regular_price'
            AND p1.post_title = p.post_title
            AND m1.meta_value < m.meta_value
     )
 GROUP BY p.post_title, m.meta_value) as x
 );
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT p.post_title, MIN(m.id) as minid, m.meta_value
FROM 
    wp_postmeta m
  ' at line 3

db <>提琴here

如果由于语法规则而无法完成操作,我将如何 选择聚合列MIN(m.id)

1 个答案:

答案 0 :(得分:1)

我认为您错过了子查询之一中的关键字。

不在其中的ID(选择x.minid (选择p.post_title,MIN(m.id)为minid,m.meta_value) ...)as x);

相关问题