SQL查询获取所有产品,类别和元数据woocommerce / wordpress

时间:2016-09-29 17:32:39

标签: mysql sql wordpress woocommerce

我试图创建一个返回参数的查询:

ID,post_title,post_content,category,ID_category,SKU,名称,股票,价格

此刻我有这个:

SELECT 
 p.ID,
 p.post_title,
 `post_content`,
 `post_excerpt`,
 t.name AS product_category,
 t.slug AS product_slug,
 tt.term_taxonomy_id AS tt_term_taxonomia,
 tr.term_taxonomy_id AS tr_term_taxonomia,
 MAX(CASE WHEN pm1.meta_key = '_price' then pm1.meta_value ELSE NULL END) as price,
 MAX(CASE WHEN pm1.meta_key = '_regular_price' then pm1.meta_value ELSE NULL END) as regular_price,
 MAX(CASE WHEN pm1.meta_key = '_sale_price' then pm1.meta_value ELSE NULL END) as sale_price,
 MAX(CASE WHEN pm1.meta_key = '_sku' then pm1.meta_value ELSE NULL END) as sku 

 FROM wp_posts p LEFT JOIN wp_postmeta pm1 ON ( pm1.post_id = p.ID)
 LEFT JOIN   wp_term_relationships AS tr ON ( tr.object_id = p.ID )
 LEFT JOIN   wp_term_taxonomy AS tt ON ( tt.taxonomy = 'product_cat' AND tr.object_id = tt.term_taxonomy_id)
 LEFT JOIN   wp_terms AS t ON ( t.term_id = tt.term_id )
 WHERE p.post_type in('product', 'product_variation') AND p.post_status = 'publish' AND p.post_content <> ''
 GROUP BY p.ID,p.post_title

它正确地给了我产品和元数据,但是我很难在这个查询中获得类别ID和类别名称,而且我无法在网上找到信息。

感谢。

2 个答案:

答案 0 :(得分:2)

object_id加入了term_taxonomy_id这没有任何意义。

以下是我认为应该如何 - 警告:我从未查询过wp数据库,只是按文档进行操作。

SELECT 
  p.ID,
  p.post_title,
  `post_content`,
  `post_excerpt`,
  t.name AS product_category,
  t.term_id AS product_id,
  t.slug AS product_slug,
  tt.term_taxonomy_id AS tt_term_taxonomia,
  tr.term_taxonomy_id AS tr_term_taxonomia,
  MAX(CASE WHEN pm1.meta_key = '_price' then pm1.meta_value ELSE NULL END) as price,
  MAX(CASE WHEN pm1.meta_key = '_regular_price' then pm1.meta_value ELSE NULL END) as regular_price,
  MAX(CASE WHEN pm1.meta_key = '_sale_price' then pm1.meta_value ELSE NULL END) as sale_price,
  MAX(CASE WHEN pm1.meta_key = '_sku' then pm1.meta_value ELSE NULL END) as sku 
FROM wp_posts p 
LEFT JOIN wp_postmeta pm1 ON pm1.post_id = p.ID
LEFT JOIN wp_term_relationships AS tr ON tr.object_id = p.ID
JOIN wp_term_taxonomy AS tt ON tt.taxonomy = 'product_cat' AND tt.term_taxonomy_id = tr.term_taxonomy_id 
JOIN wp_terms AS t ON t.term_id = tt.term_id
WHERE p.post_type in('product', 'product_variation') AND p.post_status = 'publish' AND p.post_content <> ''
GROUP BY p.ID,p.post_title

答案 1 :(得分:2)

FWIW

我自己也开始做同样的事情。我想添加产品类别,Google 将我引导到这里。

以下代码还显示了变体产品,而不仅仅是变体的 post/product main/root:

SELECT 
post_id,
t.name AS product_category,
IF(p.post_parent = 0, p.ID, p.post_parent) AS post_parent,
(SELECT post_title FROM wp_posts WHERE id = pm.post_id) AS title,
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_price" LIMIT 1) AS price, 
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_regular_price" LIMIT 1) AS "regular price", 
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_stock" LIMIT 1) AS stock, 
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_stock_status" LIMIT 1) AS "stock status", 
IFNULL((SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_sku" LIMIT 1),
    (SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_custom_field" LIMIT 1)) as SKU,
IFNULL(SUBSTR( (SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_product_attributes" LIMIT 1),
    INSTR((SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_product_attributes" LIMIT 1), "is_variation")+16,1),1) AS "variation"

FROM `wp_postmeta` AS pm
JOIN wp_posts AS p ON p.ID = pm.post_id
JOIN wp_term_relationships AS tr ON tr.object_id = IF(p.post_parent = 0, p.ID, p.post_parent)
JOIN wp_term_taxonomy AS tt ON tt.taxonomy = 'product_cat' AND tt.term_taxonomy_id = tr.term_taxonomy_id 
JOIN wp_terms AS t ON t.term_id = tt.term_id

WHERE meta_key in ("_product_version")
AND p.post_status in ("publish")
AND IFNULL(SUBSTR((SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_product_attributes" LIMIT 1),
INSTR((SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = "_product_attributes" LIMIT 1), "is_variation")+16,1),0)=0