May I know why I am getting error
Unknown column 'wp_postmeta.post_ID' in 'field list'
Here are the tables in question:
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');
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);
Here is the query:
SELECT wp_postmeta.post_ID, wp_posts.post_title, wp_postmeta.meta_value FROM (
SELECT wp_posts.post_title, MIN(wp_postmeta.meta_value) AS minprice
FROM wp_postmeta
JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID
WHERE wp_posts.post_type = 'Product'
GROUP BY wp_posts.post_title
)
as x inner join
(
SELECT wp_postmeta.post_id, wp_posts.post_title, wp_postmeta.meta_value
FROM wp_postmeta
JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID
WHERE wp_posts.post_type = 'Product'
ORDER BY wp_posts.post_title, wp_postmeta.meta_value
)
as f on x.post_title = f.post_title and f.meta_value = x.minprice
In case you're interested, I'm adapting this code from this tutorial that will give you the context of what I am doing.
you have to use subquery alias name in selection as you used subquery
SELECT f.post_ID, x.post_title, f.meta_value
FROM (
SELECT wp_posts.post_title, MIN(wp_postmeta.meta_value) AS minprice
FROM wp_postmeta
JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID
WHERE wp_posts.post_type = 'Product'
GROUP BY wp_posts.post_title
)
as x inner join
(
SELECT wp_postmeta.post_id, wp_posts.post_title, wp_postmeta.meta_value
FROM wp_postmeta
JOIN wp_posts ON wp_postmeta.post_ID=wp_posts.ID
WHERE wp_posts.post_type = 'Product'
ORDER BY wp_posts.post_title, wp_postmeta.meta_value
)
as f on x.post_title = f.post_title and f.meta_value = x.minprice
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.