2017-12-18 286 views
1

我試圖找出哪些產品缺少具有數以萬計產品的網站上的供應商的問題。獲取WooCommerce中沒有特定分類標準的產品列表

我需要一個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 

我試過很多東西並不能得到它的工作。

回答

1

問題是與您查詢的是,你不能排除有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 

另一個版本,這將是一個快一點,如果你知道所有的suppliersterm_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 

希望這有助於!

相關問題