gpt4 book ai didi

mysql - 获取 WooCommerce 中没有特定分类的产品列表

转载 作者:行者123 更新时间:2023-11-29 07:37:58 24 4
gpt4 key购买 nike

我遇到了一个问题,试图在一个拥有数万种产品的网站上找出哪些产品缺少供应商。

我需要一个 MySQL 查询来通过 phpmyadmin 确定哪些产品缺少分类“供应商”。

到目前为止,我已经从各种答案中拼凑了这个:

SELECT *
FROM wp_posts wp
INNER JOIN wp_postmeta wm ON (wm.`post_id` = wp.`ID`)
INNER JOIN wp_term_relationships wtr ON (wp.`ID` = wtr.`object_id`)
INNER JOIN wp_term_taxonomy wtt ON (wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`)
INNER JOIN wp_terms wt ON (wt.`term_id` = wtt.`term_id`)
AND wtt.`taxonomy` = 'suppliers'
WHERE post_type = 'product'
GROUP BY wp.ID

我已经尝试了很多东西,但无法让它工作。

最佳答案

您查询的问题是您没有排除具有 suppliers 的产品,该产品具有其他一些属性,因此您可以使用 NOT EXISTNOT IN,我使用 NOT EXIST 编写了查询。

SELECT *
FROM wp_posts wp
INNER JOIN wp_postmeta wm ON wm.`post_id` = wp.`ID`
INNER JOIN wp_term_relationships wtr ON (wp.`ID` = wtr.`object_id`)
INNER JOIN wp_term_taxonomy wtt ON wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`
INNER JOIN wp_terms wt ON wt.`term_id` = wtt.`term_id`
WHERE post_type = 'product'
AND NOT EXISTS
(SELECT `object_id`
FROM `wp_term_relationships` AS wtr_inner
WHERE `term_taxonomy_id` IN
(SELECT term_taxonomy_id
FROM `wp_term_taxonomy`
WHERE `taxonomy` = 'suppliers')
AND wtr.object_id = wtr_inner.object_id )
GROUP BY wp.ID

如果你知道所有的供应商,另一个版本会更快一点 term_taxonomy_id 所以你可以将上面的查询修改为

SELECT *
FROM wp_posts wp
INNER JOIN wp_postmeta wm ON wm.`post_id` = wp.`ID`
INNER JOIN wp_term_relationships wtr ON (wp.`ID` = wtr.`object_id`)
INNER JOIN wp_term_taxonomy wtt ON wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`
INNER JOIN wp_terms wt ON wt.`term_id` = wtt.`term_id`
WHERE post_type = 'product'
AND NOT EXISTS
(SELECT `object_id`
FROM `wp_term_relationships` AS wtr_inner
WHERE `term_taxonomy_id` IN (27,28,29) -- replace it will all suppliers term_taxonomy_id
AND wtr.object_id = wtr_inner.object_id )
GROUP BY wp.ID

希望这对您有所帮助!

关于mysql - 获取 WooCommerce 中没有特定分类的产品列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47863563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com