I'm creating a site using WooCommerce and I would like to integrate it with my warehouse.
I need to convert a table that looks like that:
----------------------------
id |post_id| meta_key|meta_value |
----------------------------
1 | 1 |_stock |1 |
2 | 1 |_price |10 |
3 | 1 |X |X |
4 | 2 |_stock |2 |
5 | 2 |_price |8 |
6 | 2 |X |X |
into:
-------------------------
|post id|_stock | _price |
-------------------------
| 1 |1 |10 |
| 2 |2 |8 |
I tried:
SELECT post_id, meta_value AS stock
FROM wp_postmeta
JOIN
(
SELECT post_id, meta_value AS regular_price
FROM wp_postmeta
WHERE wp_postmeta.meta_key = '_regular_price'
)
WHERE wp_postmeta.meta_key = '_stock'
ORDER BY post_id ASC
and got:
'#1248 - Every derived table must have its own alias
Is there any way that avoids create table
?
I've just started my adventure with sql and sorry if the answer is obvious. And sorry if the title is misleading.
Thank You in advance.
Just use conditional aggregation:
select post_id,
max(case when meta_key = '_stock' then meta_value end) as stock,
max(case when meta_key = '_regular_price' then meta_value end) as regular_price
from wp_postmeta
where meta_key in ('_stock', '_regular_price')
group by post_id;
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.