一个mysql查询中有两个不同的限制是可能的吗? (多阵列结果)

时间:2013-01-25 22:36:29

标签: php mysql

我的表

id Category Name
1  Sports   name1
2  Food     name2
3  Sports   name3
4  Social   name4
5  TV       name5
6  Food     name6
7  Sports   name7
8  TV       name8

正如您所看到的,每个元素名称都有一个类别,目前我的查询是:

SELECT * FROM interests ORDER BY category DESC;

然后在PHP中,我按类别分组,然后使用各自的名称字段计算前3名。我想在MySQL中做同样的事情(如果可能的话),因为现在我有很多数据而且我不想每次查询所有表格,我想将类别数量限制在前3位和名字为2。

我尝试过:SELECT *,COUNT(*) as catnumber FROM interests GROUP BY category ORDER BY catnumber DESC LIMIT 5但是只要给我一个名字元素的前5个类别(如果可用,我需要最多2个)。所以结果应该像多阵列结果:

Categories = array('Sports' => array('name1', 'name3','name7'), 'Food' => array('name2','name6'), 'TV' => array('name5','name8'))

这可能吗?

4 个答案:

答案 0 :(得分:0)

MySQL不会返回“多阵列”结果。

但是您可以使用如下查询获得三个类别,每个类别最多两个名称:

SELECT c.category
     , (SELECT n1.name
          FROM interests n1
         WHERE n1.category = c.catetory
         ORDER BY n1.name
         LIMIT 0,1
       ) AS n1
     , (SELECT n2.name
          FROM interests n2
         WHERE n2.category = c.catetory
         ORDER BY n2.name
         LIMIT 1,1
       ) AS n2
  FROM ( SELECT t.category
              , COUNT(1) AS cnt
           FROM interests t
          GROUP BY t.category
          ORDER BY cnt DESC
              , t.category
          LIMIT 3
       ) c

那应该返回这样的结果集:

    category  n1     n2 
    --------  ----   ----
    Sports    name1  name3 
    Food      name2  name6
    TV        name5  name8

也可以在不同的行上获取名称,但查询稍微复杂一些:

SELECT c.category
     , CASE WHEN i.i = 1 THEN 
       (SELECT n1.name
          FROM interests n1
         WHERE n1.category = c.catetory
         ORDER BY n1.name
         LIMIT 0,1
       ) ELSE
       (SELECT n2.name
          FROM interests n2
         WHERE n2.category = c.catetory
         ORDER BY n2.name
         LIMIT 1,1
       ) END AS `name`
  FROM ( SELECT t.category
              , COUNT(1) AS cnt
           FROM interests t
          GROUP BY t.category
          ORDER BY cnt DESC
              , t.category
          LIMIT 3
       ) c
 CROSS
  JOIN (SELECT 1 AS i UNION ALL SELECT 2) i
 ORDER BY c.cnt DESC, t.category, i.i

为避免返回NULL值,可以在ORDER BY

之前添加HAVING子句
HAVING n IS NOT NULL

应该返回:

    category  n
    --------  ----
    Sports    name1
    Sports    name3
    Food      name2
    Food      name6
    TV        name5
    TV        name8

答案 1 :(得分:0)

您可以使用GROUP_CONCAT执行此操作并展开,

上的类别
SELECT `category`,
       count(id) AS entries,
       GROUP_CONCAT(NAME)
FROM `interests` l1
GROUP BY `category`
ORDER BY entries DESC LIMIT 3;

答案 2 :(得分:0)

您无法直接从MySQL查询中获取多维数组。如果我理解你的表结构和你想要的输出的描述,你可以用这样的东西来近似你的数组:

SELECT id,
       Category,
       (SELECT 
        GROUP_CONCAT(Name SEPARATOR ',') 
        FROM interests AS sint 
        WHERE sint.Category = interests.Category) as Name,
       (SELECT 
        COUNT(Name) 
        FROM interests AS csint 
        WHERE csint.Category = interests.Category) as count
FROM interests 
GROUP BY Category
HAVING count >= 2
ORDER BY count DESC,CATEGORY
LIMIT 3

这给了你:

id  Category    Name           count
1   Sports    name1,name3,name7     3
2   Food      name2,name6           2
5   TV        name5,name8           2

要按照以下评论中的要求对整个表进行排序,请使用:

SELECT id,
       Category,
       Name,
       (SELECT 
        COUNT(Name) 
        FROM interests AS csint 
        WHERE csint.Category = interests.Category) as count
FROM interests 
ORDER BY count DESC,Category

答案 3 :(得分:0)

试试这个:

    SELECT a.category, SUBSTRING_INDEX(GROUP_CONCAT(distinct(a.NAME)   SEPARATOR ', '),",",2) AS `Top names`
    FROM MyTable AS A JOIN 
        (SELECT category 
        FROM MyTable 
        GROUP BY category 
        ORDER BY count(category) DESC 
        LIMIT 3) AS C
    ON A.category = C.category 
    GROUP BY a.category 
    ORDER BY  count(a.category) DESC, count(A.name)  DESC ;

SQL FIDDLE Demo

类别顶部尺寸目前为3,名称的顶部尺寸当前为2.通过更改值,您可以调整顶部的尺寸。请注意,当类别和名称具有抽奖时,将不会显示第二个类别或名称。