1

我使用以下查询从类别表中选择类别名称

select Category.Name 
  from Product 
    inner  join ProductCategory on ProductCategory.PID=Product.PID 
    inner join ProductMaterial on ProductMaterial.PID=Product.PID 
    left join Category on Category.NodeId=ProductCategory.CID 
  where PID in('2233','4432','5665','1252')  
  group by ProductCategory.CID, ProductMaterial.MID,Category.DanishName

查询正在运行 此查询的结果是

Electronics
Electronics
Electronics
Home and Garden

我只想选择最多的类别名称,这里我只需要电子产品。如何获得这个。在此先感谢您的帮助...>>

4

1 回答 1

2

尝试这个:

MySQL

SELECT A.name, COUNT(A.name) nameCnt
FROM (SELECT C.Name 
      FROM Product P 
      INNER JOIN ProductCategory PC ON PC.PID=P.PID 
      INNER JOIN ProductMaterial PM ON PM.PID=P.PID 
      INNER JOIN Category C ON C.NodeId=PC.CID 
      WHERE PID IN('2233','4432','5665','1252')  
      GROUP BY PC.CID, PM.MID, C.DanishName 
    ) AS A 
GROUP BY A.name 
ORDER BY nameCnt DESC LIMIT 1;

SQL 服务器

SELECT TOP 1 A.name, COUNT(A.name) nameCnt
FROM (SELECT C.Name 
      FROM Product P 
      INNER JOIN ProductCategory PC ON PC.PID=P.PID 
      INNER JOIN ProductMaterial PM ON PM.PID=P.PID 
      INNER JOIN Category C ON C.NodeId=PC.CID 
      WHERE PID IN('2233','4432','5665','1252')  
      GROUP BY PC.CID, PM.MID, C.DanishName 
    ) AS A 
GROUP BY A.name 
ORDER BY nameCnt DESC;
于 2014-01-22T10:02:43.253 回答