计算每个不同项目的数量 - 试图找到最常见的项目

时间:2017-08-26 02:14:55

标签: mysql

我正在努力寻找最后10个最常见的条目。我可以找到不同的条目,但我不知道如何计算每个条目的数量。

最后10个条目是

table
desk
desk
desk
chair
table
chair
desk
table
chair

以下SQL查询可以找到不同的条目

SELECT DISTINCT entry FROM `records` WHERE  custid ='464' LIMIT 10

table
chair
desk

我试过了

SELECT COUNT(entry) FROM 
  (SELECT DISTINCT entry FROM `records` 
  WHERE  custid ='464' LIMIT 10)

以上似乎不起作用。我正在寻找的是一个将产生

的SQL查询
table (3)
desk (4)
chair (3)

有人能找到办法吗?

提前感谢您的帮助

2 个答案:

答案 0 :(得分:0)

// Check if ppTracker is 1 and allow the user to pause
    if (ppTracker == 1) {
        // Change the image icon
        btnPlaynPause.setIcon(unpressPause);
        //songName = musicAccess.getSongName();
        //displaySong.setText(songName);
        displaySong.setText(music.get(mTracker).getSongName());
        player.play();
        System.out.println("Size "+player);

        // Check if ppTracker is 2 and allow the user to resume or play
    } else if (ppTracker == 2) {
        // Change the image icon
        btnPlaynPause.setIcon(unpressPlay);
        player.pause();
        // Reset the tracker back to 0
        ppTracker = 0;
    }

答案 1 :(得分:0)

您需要的是GROUP BYhttps://www.w3schools.com/sql/sql_groupby.asp) 这个特殊问题可以解决(考虑条目是包含表,esk等的列...)      SELECT entry,COUNT(*) FROM records WHERE custid ='464' GROUP BYentry LIMIT 10

直接将LIMIT应用于GROUP BY MySQL会限制每组的行数,以便实际查询

`SELECT entry,COUNT(*) FROM (Select entry from records WHERE custid ='464' limit 10) GROUP BY entry`