2016-10-25 52 views
1

我正在使用woocomerce平臺,並且有重複的產品名稱條目。現在,我想查看由用戶組輸入的具有特定類別名稱的重複「product_name」的categories_id,categories_name,products_name。如何在woocomerce中使用sql選擇重複產品

正如我在數據庫表中看到,這裏有需要的表來查看結果:根據下表模式

-------------------------- 
    | categories_description | 
    -------------------------- 
    categories_id(PK) 
    categories_name 

    ----------------------- 
    | products_description | 
    ----------------------- 
    products_id 
    products_name 


    ---------------------- 
    |products_to_categories | 
    ----------------------- 
    products_id 
    categories_id 

我的查詢:

SELECT prod.products_id, `products_name`, categories_name,  
    prodcat.categories_id , count(*) c 
    FROM 
    (products_description AS prod INNER JOIN products_to_categories AS    
    prodcat ON prod.products_id=prodcat.products_id) 
    INNER JOIN 
    categories_description AS catdesc ON  
    catdesc.categories_id=prodcat.categories_id AND categories_name= 'CARD 
    HOLDERS' 
    GROUP BY products_name 
    order by categories_name 

我的查詢的問題是,它無法顯示條目列表重複產品名稱

example Inputs: 

products_description 
--------------------------- 
products_id | products_name 
     1   pens 
     2   pens 
     3   cars 
     4   cars 
     5   cars 
     6   burgers 

products_to_categories 
    ------------------------------- 
    products_id | categories_id 
     1     1 
     2     1 
     3     2 
     4     2 
     5     2 
     6     3  

    categories_description 
    ------------------------------ 
    categories_id | categories_name 
     1    School Supply 
     2    Vehicles 
     3     Foods 

    ================================= 
    Expected Output 
    ================================= 
    products_name categories_name categories_id  total_count 
     pens   School Supply   1    2 
     cars   Vehicles    2    3 
     burgers   Foods    3    1 

看看總數。這取決於products_description表中出現的次數

請幫忙。

+0

我看不到在子查詢了'SELECT'。你的查詢是否運行?向我們展示樣品輸入和所需輸出。 –

+0

請參閱樣本修改後的問題。, – smzapp

回答

0

這裏是應該工作 ...

SELECT t1.products_name, 
     MAX(t3.categories_name) AS catgories_name, 
     MAX(t2.categories_id) AS categories_id, 
     COUNT(*) AS total_count 
FROM products_description t1 
INNER JOIN products_to_categories t2 
    ON t1.products_id = t2.products_id 
INNER JOIN categories_description t3 
    ON t2.categories_id = t3.categories_id 
GROUP BY t1.products_name 

...但即使此查詢應給予正確的輸出,你的數據庫設計有一些缺陷的查詢。重複產品不應將每個產品條目映射到類別表中的單獨條目,而應包含重複的ID,這些ID都映射到相同的類別。簡而言之,你的數據庫沒有正確的標準化,所以我的查詢採取了一些不好的事情,比如假設所有的值都是相同的,取某些列的MAX

這是我會怎麼修改架構:

products_description 

╔════╦═════════════╦═══════════════╗ 
║ id ║ products_id ║ products_name ║ 
╠════╬═════════════╬═══════════════╣ 
║ 1 ║  1  ║  pens  ║ 
║ 2 ║  1  ║  pens  ║ 
║ 3 ║  2  ║  cars  ║ 
║ 4 ║  2  ║  cars  ║ 
║ 5 ║  2  ║  cars  ║ 
║ 6 ║  3  ║  burgers ║ 
╚════╩═════════════╩═══════════════╝ 

products_to_categories 

╔═════════════╦═══════════════╗ 
║ products_id ║ categories_id ║ 
╠═════════════╬═══════════════╣ 
║  1  ║  1  ║ 
║  2  ║  2  ║ 
║  3  ║  3  ║ 
╚═════════════╩═══════════════╝ 

你的第三個表可以保持原樣:

categories_description 

╔═══════════════╦═════════════════╗ 
║ categories_id ║ categories_name ║ 
╠═══════════════╬═════════════════╣ 
║  1  ║ School Supply ║ 
║  2  ║ Vehicles  ║ 
║  3  ║ Foods   ║ 
╚═══════════════╩═════════════════╝