如何使用sql选择woocomerce中的重复产品

时间:2016-10-25 03:35:01

标签: mysql

我正在使用woocomerce平台,我有重复的产品名称条目。现在,我希望看到用户组按特定类别名称输入的duplicate“product_name”的categories_id,categories_name,products_name。

正如我在数据库表中看到的,以下是查看结果所需的表:

基于下面的表格架构:

  --------------------------
  | 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表中的出现次数

请帮忙。

1 个答案:

答案 0 :(得分: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          ║
╚═══════════════╩═════════════════╝