SQL从一个表中选择从另一个表中计算行,内部联接&计数

时间:2017-06-12 11:56:41

标签: sql count inner-join

我有两张桌子:

player:

+----+--------+----------+---------------+
| id | name   | country  | military_rank |
+----+--------+----------+---------------+
|  1 | John   | Portugal |            26 |
|  2 | Ninja  | Japan    |            23 |
|  3 | Elone  | Spain    |            28 |
|  4 | Markus | China    |             5 |
+----+--------+----------+---------------+

unit

+----+-----------+-------------+--------+
| id | player_id | unit        | number |
+----+-----------+-------------+--------+
|  1 |         1 | bazooka     |      6 |
|  2 |         2 | bazooka     |      2 |
|  3 |         3 | bazooka     |     16 |
|  4 |         4 | bazooka     |      2 |
|  5 |         1 | machine gun |     10 |
|  6 |         4 | missile     |      2 |
+----+-----------+-------------+--------+

我需要一个SQL查询来提取军事行程高于25且有5个或更多bazooca单位的玩家(player表)的名称。

这是我试过的:

SELECT COUNT(unit.unit) as num_baz, player.name as name 
FROM player 
INNER JOIN unit 
    ON player.id = unit.player_id 
GROUP BY unit.unit;

ERROR:

  

错误1055(42000):SELECT列表的表达式#2不在GROUP BY中   子句并包含非聚合列'Chill_Time.player.name'   它在功能上不依赖于GROUP BY子句中的列;   这与sql_mode = only_full_group_by

不兼容

3 个答案:

答案 0 :(得分:0)

我想你可能正在寻找这样的东西:

kibana

答案 1 :(得分:0)

快速猜测:

SELECT player.name from player INNER JOIN unit on player.id=unit.player_id where player.military_rank < 25 AND unit.number <= 5 AND unit.unit = 'bazooka' ;

答案 2 :(得分:0)

尝试下面的一个:

    SELECT COUNT(dbo.unit.unit) AS num_baz, dbo.player.name
    FROM dbo.player INNER JOIN dbo.unit ON dbo.player.id = dbo.unit.player_id
    GROUP BY dbo.player.name, dbo.player.military_rank, dbo.unit.number, dbo.unit.unit
    HAVING (dbo.player.military_rank > 25) AND (dbo.unit.number >= 5) AND (dbo.unit.unit = N' bazooka')