代码之家  ›  专栏  ›  技术社区  ›  ptrcao

用别名从子查询中选择列会产生错误

  •  0
  • ptrcao  · 技术社区  · 6 年前

    特别是,我希望从子查询中按别名选择一列,但这会产生一个错误。我在下面的完整查询中特别提到了这一行:

        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
    

    小提琴 here

    如果因为语法规则而无法完成,如何 我选择聚合列 MIN(m.id) ?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Aniket Goel    6 年前