簡體   English   中英

如何將 SELECT 查詢轉換為 DELETE 以刪除不需要的額外分類?

[英]How I can transform SELECT query to DELETE for remove extra taxonomy unwanted?

我嘗試此查詢顯示重復的產品,我的網絡中的 term_taxonomy_id 對應於產品的庫存(無,中,高,低)圖像底部*

我想將它轉換為一個查詢 DELETE... 它將刪除那些重復的查詢,並僅保留最新的 term_order,因為如果產品有兩個或更多的股票標簽,則不會在網絡上顯示。

他們只需要有一個標簽object_id=1 有 high,none | object_id=3 沒有,中等這是我的問題。

table: wp_term_relationships
+-----------+------------------+-----------+
| object_id | term_taxonomy_id | term_order|
+-----------+------------------+-----------+
| 1         | high             | 0         |
| 1         | none             | 1         |<---delete none
| 1         | bbb              | 10        |
| 1         | ccc              | 10        |
| 2         | high             | 0         |
| 2         | aaa              | 11        |
| 2         | bbb              | 11        |
| 2         | ccc              | 11        |
| 3         | none             | 0         |
| 3         | medium           | 1         |<---delete medium
| 3         | high             | 12        |<---delete high
| 4         | jjj              | 12        |
| 5         | kkk              | 12        |
| 5         | lll              | 12        |
| 5         | high             | 12        |
| *         | *                | *         |
+-----------+------------------+-----------+

表格的圖像因為在移動版本中是不正確的

SELECT object_id,term_taxonomy_id,term_order 
FROM wp_term_relationships 
WHERE (    term_taxonomy_id = none
        OR term_taxonomy_id = medium
        OR term_taxonomy_id = high
        OR term_taxonomy_id = low)
GROUP BY object_id
HAVING COUNT(*)>1

*有問題的圖片: https : //ibb.co/k5XLCdS

這是你需要的查詢,做一個測試

delete  wp_term_relationships  
from  wp_term_relationships 
 inner join ( 
SELECT object_id,term_taxonomy_id,term_order 
FROM  wp_term_relationships a
WHERE (    term_taxonomy_id = 'none'
        OR term_taxonomy_id = 'medium'
        OR term_taxonomy_id = 'high'
        OR term_taxonomy_id = 'low') and (
select sum(if(v.term_taxonomy_id in  ('high','none','medium','low') , 1, 0)) from wp_term_relationships as v where v.object_id = a.object_id) > 1  
except
SELECT object_id,term_taxonomy_id,term_order
FROM  wp_term_relationships 
WHERE (    term_taxonomy_id = 'none'
        OR term_taxonomy_id = 'medium'
        OR term_taxonomy_id = 'high'
        OR term_taxonomy_id = 'low')
GROUP BY object_id
HAVING COUNT(*)>1 ) y on 
wp_term_relationships.object_id = y.object_id and 
wp_term_relationships.term_taxonomy_id = y.term_taxonomy_id and
wp_term_relationships.term_order = y.term_order

db<>小提琴鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM