简体   繁体   English

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

[英]Selecting a column from a subquery by its alias name generates error

In particular, I want to select a column from a subquery by its alias name but this generates an error. 特别是,我想通过子查询的别名来选择列,但这会产生错误。 I refer to this line in particular, from the full query below: 我在下面的完整查询中特别提到了这一行:

    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) ); 
\n \n
 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 ); 
\n \n
 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'); 
\n \n
 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); 
\n \n
 -- 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 ); 
\nYou have an error in your SQL syntax; 您的SQL语法有误; 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 检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在'SELECT p.post_title,MIN(m.id)附近使用mini,m.meta_value\nFROM  \n    wp_postmeta m wp_postmeta m\n  ' at line 3 '在第3行\n

db<>fiddle here db <> 在这里拨弄

If it can't be done because of syntax rules, how would I select the aggregate column MIN(m.id) ? 如果它不能因为语法规则来完成,我怎么选择聚合列MIN(m.id)

I think you have missed from keyword in one of the subquery. 我认为您错过了子查询之一中的关键字。

WHERE id NOT IN (SELECT x.minid from (SELECT p.post_title, MIN(m.id) as minid, m.meta_value ...) as x ); 不在其中的ID( (SELECT p.post_title,MIN(m.id)作为minid,m.meta_value ...)作为x中选择x.minid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM