如果在设置为0的列中不存在该值

时间:2019-07-25 13:44:34

标签: mysql

我正在尝试获取每场比赛的球员人数,如果有一场比赛没有球员,则将其设置为0。

该表是用C ++填充的,因此作业播放器需要以与C ++枚举相同的顺序显示。

播放器表存储了所有来自播放器的数据,并且job列不是NULL,因为要创建播放器,必须选择作业。如果没有人参加比赛,我想将比赛计数显示为0。

我已经尝试过COALESCE,但是没有运气。

还看到了有关使用JOIN的其他主题,但不适用于此处。我只有一张桌子可以工作。

我当前的查询:

SELECT
player.job AS race,
Count(player.job) AS count_races
FROM
player
GROUP BY
race
ORDER BY
FIELD(race, 0, 4, 5, 1, 2, 6, 7, 3, 8, 9) ASC;

我得到的是什么

race    count_races
0       5
5       3
1       2
2       6
6       2
3       4

我期望得到的:

race    count_races
0       5
4       0
5       3
1       2
2       6
6       2
7       0
3       4
8       0
9       0

我的表结构:

CREATE TABLE `player`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `account_id` int(11) NOT NULL DEFAULT 0,
  `name` varchar(24) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'NONAME',
  `job` tinyint(2) UNSIGNED NOT NULL DEFAULT 0,
  `dir` tinyint(2) NOT NULL DEFAULT 0,
  `x` int(11) NOT NULL DEFAULT 0,
  `y` int(11) NOT NULL DEFAULT 0,
  `z` int(11) NOT NULL DEFAULT 0,
  `map_index` int(11) NOT NULL DEFAULT 0,
  `exit_x` int(11) NOT NULL DEFAULT 0,
  `exit_y` int(11) NOT NULL DEFAULT 0,
  `exit_map_index` int(11) NOT NULL DEFAULT 0,
  `hp` int(4) NOT NULL DEFAULT 0,
  `playtime` int(11) NOT NULL DEFAULT 0,
  `level` tinyint(2) UNSIGNED NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`, `last_play`) USING BTREE,
  UNIQUE INDEX `name_idx`(`name`) USING BTREE,
  INDEX `account_id_idx`(`account_id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 1114 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;

1 个答案:

答案 0 :(得分:1)

  

引用Karbust的话(评论)
  我编辑了问题,我不相信其他问题可以回答   矿。还添加了我的播放器表结构并解释了一些   更好。

是的,但是诀窍是在COUNT(*)中添加了一个相关的子查询。

SELECT 
    search_filter.job
  , (SELECT
       COUNT(*)
     FROM player player_inner
     WHERE player_inner.job = player_outer.job
    ) AS count_races
FROM (
  SELECT 
   0 AS job
  UNION
  SELECT
    4 AS job
  UNION
  SELECT
    5 AS job  
  UNION
  SELECT
    1 AS job
  UNION
  SELECT
    2 AS job   
  UNION
  SELECT
    6 AS job   
  UNION
  SELECT
    7 AS job  
  UNION
  SELECT
    3 AS job   
  UNION
  SELECT
    8 AS job  
  UNION
  SELECT
    9 AS job    
  # [...]
) AS search_filter
LEFT JOIN
 player player_outer
ON
 search_filter.job = player_outer.job
ORDER BY
  FIELD(search_filter.job, 0, 4, 5, 1, 2, 6, 7, 3, 8, 9) ASC; 

结果(由于表为空)

| job | count_races |
| --- | ----------- |
| 0   | 0           |
| 4   | 0           |
| 5   | 0           |
| 1   | 0           |
| 2   | 0           |
| 6   | 0           |
| 7   | 0           |
| 3   | 0           |
| 8   | 0           |
| 9   | 0           |

请参阅demo

没有相关子查询的更优化版本是。

SELECT 
    search_filter.job
  , COUNT(CASE WHEN player.job IS NOT NULL THEN 1 ELSE NULL END) AS count_races
FROM (
  SELECT 
   0 AS job
  UNION
  SELECT
    4 AS job
  UNION
  SELECT
    5 AS job  
  UNION
  SELECT
    1 AS job
  UNION
  SELECT
    2 AS job   
  UNION
  SELECT
    6 AS job   
  UNION
  SELECT
    7 AS job  
  UNION
  SELECT
    3 AS job   
  UNION
  SELECT
    8 AS job  
  UNION
  SELECT
    9 AS job    
  # [...]
) AS search_filter
LEFT JOIN
 player
ON
 search_filter.job = player.job
GROUP BY 
 search_filter.job
ORDER BY
  FIELD(search_filter.job, 0, 4, 5, 1, 2, 6, 7, 3, 8, 9) ASC

请参阅demo