0

我的桌子

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 DESCLIMIT 5 但只给我前 5 个类别,其中包含一个名称元素(如果可用,我最多需要两个类别)。所以结果应该类似于多数组结果:

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

这可能吗?

4

4 回答 4

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
于 2013-01-26T00:48:37.813 回答
0

您可以使用 GROUP_CONCAT 来执行此操作并在,

SELECT `category`,
       count(id) AS entries,
       GROUP_CONCAT(NAME)
FROM `interests` l1
GROUP BY `category`
ORDER BY entries DESC LIMIT 3;
于 2013-01-26T00:28:06.183 回答
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

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

    类别 n1 n2
    -------- ---- ----
    运动名称1 名称3
    食物名称2名称6
    电视名称5 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

那应该返回:

    n 类
    -------- ----
    运动名称1
    运动名称3
    食品名称2
    食品名称6
    电视名称5
    电视名称8
于 2013-01-26T00:18:05.320 回答
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 演示

类别顶部大小当前为 3,名称的顶部大小当前为 2。通过更改值,您可以调整顶部的大小。请注意,当类别和名称抽签时,不会显示第二个类别或名称。

于 2013-01-26T02:33:54.793 回答