【发布时间】:2020-04-05 23:21:09
【问题描述】:
我需要帮助来完成对 Woocommerce 产品列表的 SQL 查询。我需要这个用于 wpdatatable sql 输入。
我搜索了一个示例代码并改编了一个找到的代码:
SELECT
p.ID,
p.post_title,
p.post_content,
p.post_excerpt,
t.name AS product_category,
t.term_id AS product_id,
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
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'
GROUP BY p.ID,p.post_title
它可以工作,但没有可变产品的产品行。它仅显示产品的主要变体。
示例: 产品牛排:我有 6 种不同的牛排作为自己的产品,价格不同,重量特征也不同。 通过我的 SQL 查询,我只能看到没有变化和变化价格的主要产品“牛排”。
表格 wp_Posts
编号 | post_title | post_type |后父 | post_name |常规价格 | -----+--------------------------------+------------ --------+-------------+------------+--------------- | 4113 |牛排 |产品 | 0 | 4119 |牛排-Lungenbraten Steak 125g | product_variation | 4113 |牛排_4 |6 | 4120 |牛排-Hüftsteak 200g | product_variation | 4113 |牛排_5 |4,4 | 4121 |牛排-Flankensteak 600g | product_variation | 4113 | steaks_6 |8,4 |表 wp_postmeta-
Post_ID |meta_key |元值 | --------+----------------+------------ --+--------- 4113 | | 4119 |属性_pa_steaks | lungenbraten-牛排| 高分辨率照片| CLIPARTO 4119 | _价钱。 | 6 | 4120 |属性_pa_steaks |牛排| 4120 | _价钱。 | 4,4 | 4121 |属性_pa_steaks |牛排| 4121 | _价钱。 | 8,4 |wp_term_relationship
object_id。 | term_taxonomy_id。 | term_order | ------------+-------------------+------------+ 4113 | 6 | 0 | 4113 | 296 | 0 | 4113 | 297 | 0 | 4113 | 298 | 0 |wp_term_taxonomy
term_taxonomy_id。 | term_id |分类 |描述。 |父级 |计数 | ------------------+------------+--------------+------ --------+--------+------+ 296 | 296 |牛排 | | 0 | 1. | 297 | 297 |牛排 | | 0 | 1. | 298 | 298 |牛排 | | 0 | 1. |wp_terms
term_id |姓名 |蛞蝓 |术语组 | ------------+--------------------------------+---------------- ----+------------+ 296 |牛排 |牛排| 0 | 297 |牛排 |牛排| 0 | 298 | Lugenbraten 牛排 | Lugenbraten 牛排 | 0 |【问题讨论】:
-
您通常 GROUP BY 与您选择的列相同,但那些作为设置函数的参数的列除外。
-
向我们展示一些示例表数据和预期结果 - 作为格式化文本,而不是图像。在开始之前看看minimal reproducible example。
-
示例:woo 可变产品:名称价格牛排变体 1:侧腹牛排 8,4 变体 2:Hüftsteak。 4,4 变体 3:Lungenbraten 6,0 结果 SQL 查询原样:牛排 8,4
-
关于jarlh对
GROUP BY的评论:你按帖子分组,每个帖子得到一个结果行。但是一个帖子可以有很多词条,那么你怎么能在帖子中选择the词条。我们希望您也可以按术语分组,以便每个帖子和术语获得一个结果行,或者仅从术语中选择聚合,例如最大 term_id 和最小 term_taxonomy_id。 -
关于样本数据:请编辑您的请求以显示它。显示每个表的数据示例。对于您的三种牛排变体:
posts表中有什么?表wp_postmeta中有什么?表中的内容terms...
标签: mysql sql woocommerce