【问题标题】:Support needed for a query查询所需的支持
【发布时间】: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


【解决方案1】:

您的查询无效,因为选择列表与GROUP BY 不匹配。即使您的 DBMS 有缺陷并且没有引发错误,我也不建议使用这样的查询。它的结果可能是任意的(例如 MySQL 中的情况)。编写一个查询,明确您真正想要选择的数据。

由于您只需要键/值表wp_postmeta 中的一个值,因此不需要聚合:

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,
  pm1.meta_value AS price
FROM wp_posts p 
LEFT JOIN wp_postmeta pm1 ON pm1.post_id = p.ID AND pm1.meta_key = '_price'
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'
ORDER BY
  CASE WHEN p.post_parent = 0 THEN id else post_parent END,
  CASE WHEN p.post_parent = 0 THEN 1 else 2 END;

【讨论】:

  • 非常感谢,它有效,该表显示所有行的价格正确,但在 post_title 列中仅显示 post_parent 4104 的文本。我怎样才能获得此行的正确 post_titles?
  • 4104 对我来说没有任何意义。您没有在示例中显示表 wp_term_relationshipswp_term_taxonomy。也许有些帖子没有条款,而您仍想显示它们?然后,您必须将所有连接转为左外连接。除此之外:查询显示存储在显示的帖子行中的帖子标题。这不是你想要的吗?
  • 对不起,我来晚了。我的意思是 4113。post_type product_variation 的正确 post_title 也在 wp_posts 中。 (我是您论坛的新手,因此我必须学习很多如何使用评论...)。
  • 查询选择p.id、p.post_title、p_post_content等,直接从表wp_posts中读取。因此,对于 ID 4113,您必须看到“牛排”,对于 ID 4119,您必须看到“Steaks-Lungenbraten Steak 125g”。您是说您看到 ID 4113 和“Steaks-Lungenbraten Steak 125g”?从您显示的数据来看,这是不可能的。因此,要么您的数据不是您认为的那样,要么您正在使用某种工具以某种方式混合行数据。后者的可能性很小,因此请检查您的表格数据。
  • 我在问题部分添加了查询结果的图片。您可以看到 product_variations 的价格是正确的,但 id 和 post_title 始终显示父数据,并且不会针对 product_variations 更改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2021-11-08
  • 2018-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多