mysql 'as' 相关

标签 mysql sql where-clause

第一段代码工作正常,并给出了我需要的结果。 我现在想做的是让它在 'as num' 上返回 3 个数字值 对于“as num”上的 3 个不同值,对于同一列上的 3 个不同位置

        SELECT COUNT(garage_list.account_id) as num, 
        garage_list.account_id, 
        garage_list.tank_id,
        player_list.clan,
        player_list.nickname,
        tank_list.level
    FROM garage_list 
        RIGHT JOIN player_list ON garage_list.account_id = player_list.account_id 
        RIGHT JOIN tank_list ON garage_list.tank_id = tank_list.tank_id

      where tank_list.level='8' 

      and player_list.clan = 'bad-1'
          GROUP BY account_id 

以上作品

需要为每个“其中tank_list.level='??'”提供一个返回值”

SELECT COUNT(garage_list.account_id) as num, 
        garage_list.account_id, 
        garage_list.tank_id,
        player_list.clan,
        player_list.nickname,
        tank_list.level
    FROM garage_list 
        RIGHT JOIN player_list ON garage_list.account_id = player_list.account_id 
        RIGHT JOIN tank_list ON garage_list.tank_id = tank_list.tank_id


      where tank_list.level='6'  //return a value for this
      where tank_list.level='8'  //return a value for this
      where tank_list.level='10'  //return a value for this

      and player_list.clan = 'bad-1'

      GROUP BY account_id 

最佳答案

您需要使用 CASE 的条件 COUNT:

SELECT COUNT(case when tank_list.level='6' then garage_list.account_id end) as num1,
       COUNT(case when tank_list.level='8' then garage_list.account_id end) as num2,
       COUNT(case when tank_list.level='10' then garage_list.account_id end) as num3, 
        garage_list.account_id, 
        garage_list.tank_id,
        player_list.clan,
        player_list.nickname,
        tank_list.level
    FROM garage_list 
        RIGHT JOIN player_list ON garage_list.account_id = player_list.account_id 
        RIGHT JOIN tank_list ON garage_list.tank_id = tank_list.tank_id


      where tank_list.level in ('6','8','10')

      and player_list.clan = 'bad-1'

      GROUP BY account_id 

并且您需要检查您的 Outer Join 逻辑是否正确。

关于mysql 'as' 相关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32971494/

相关文章:

mysql - 获取最新记录

mysql - 在 MySQL 中进行子分区时出现语法错误

mysql - SQL查询顺序按3个国家然后按名称

MySQL 搜索一个字段但删除所有非数字字符

mysql - 如何从表中选择不同的值

php - 如何将xml文件插入到mysql表中

mysql - SQL Select Query中的Where条件算法

php - 如何使用 Apache、Php 和 Mysql 制作 "portable"web 应用程序?

mysql - MySQL中时间戳值的内部表示是什么

mysql - SQL查询以生成一年中每个月的计数