我正在使用MySQL查询列出所有产品插件变体(样本),我能够做到这一点,以及列出与变体相关联的产品。我遇到的问题是列出产品类别ID。
我没有正式的MySQL培训,所以任何有关性能等方面的帮助和建议也都很有价值!
以下是我用来获取插件变体及其产品列表的代码,只需获取这些产品的类别ID即可。
$prod_attributes_sql = 'SELECT DISTINCT wt.slug AS term_slug, wtr.term_taxonomy_id, wpp.post_title AS wpost_title, wt.term_id AS wpt_terms, cpt.term_id AS cpt_terms
FROM '
. " {$wc_price_table->cat_price_table_name} cpt"
. ' RIGHT OUTER JOIN wp_terms wt ON cpt.term_id=wt.term_id'
. ' INNER JOIN wp_term_relationships wtr ON wt.term_id = wtr.term_taxonomy_id'
. ' INNER JOIN wp_posts wpp ON wtr.object_id = wpp.ID'
. ' RIGHT OUTER JOIN wp_term_taxonomy ttx on wt.term_id = ttx.term_id'
. ' WHERE ttx.taxonomy IN (\'' . implode("','", $slugsnews) . '\')'
. ' ORDER BY wt.name';
$prod_attributes = $wpdb->get_results($prod_attributes_sql) or die(mysql_error());
输出插件变体列表及其关联的产品。但是,我将如何包含这些产品的类别ID。
如果我需要添加任何进一步的解释或代码,请告诉我。
提前致谢!
答案 0 :(得分:0)
我最终不得不更改查询以包含$term_id
,使用以下foreach
来获取条款:
$term_id = array();
foreach ($product_categories as $key => $category) {
foreach ($category['terms'] as $term) {
$term_id[] = $term->term_id;
}
}
并且包含在我的sql中:
$prod_attributes_sql = 'SELECT DISTINCT wt.slug AS term_slug, wt.name, ttx.taxonomy, wtr.term_taxonomy_id, wpp.post_title AS wpost_title, wt.term_id AS wpt_terms, cpt.term_id AS cpt_terms
FROM '
. " {$wc_price_table->cat_price_table_name} cpt"
. ' RIGHT OUTER JOIN wp_terms wt ON cpt.term_id=wt.term_id'
. ' INNER JOIN wp_term_relationships wtr ON wt.term_id = wtr.term_taxonomy_id'
. ' INNER JOIN wp_posts wpp ON wtr.object_id = wpp.ID'
. ' RIGHT OUTER JOIN wp_term_taxonomy ttx on wt.term_id = ttx.term_id'
. ' WHERE cpt.term_id IN(' . implode(',', $term_id) . ')'
. ' OR ttx.taxonomy IN (\'' . implode("','", $slugsnews) . '\')'
. ' OR ttx.term_taxonomy_id = cpt.term_id'
. ' ORDER BY wpp.post_title';
$prod_attributes = $wpdb->get_results($prod_attributes_sql) or die(mysql_error());
这很有效。