MYSQL 合并、计数和分离具有相同 id 但不同值的行的实例

标签 mysql

目前我有一个看起来像这样的查询

Start

查询是这样写的:

SELECT username, exerciseId, frustrated, bored 
FROM affecttutor.selfreportfrustration 
order by username;

我正在努力让它看起来像这样

Finish

可能的方法是什么?感谢您的宝贵时间!

最佳答案

您可以尝试使用条件聚合

select 
   username,exerciseid,
   count(case when frustrated='Y' then 1 end) as frustrated,
   count(case when frustrated<>'Y' then 1 end) as notfrustrated,
   count(case when bored<>'N' then 1 end) as bored ,
   count(case when bored='N' then 1 end) as notbored
from affecttutor.selfreportfrustration
group by username,exerciseid

关于MYSQL 合并、计数和分离具有相同 id 但不同值的行的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53315761/

相关文章:

mysql - 基准测试期间有趣的 MySQL 行为

php - 用于使用 php 从 mysql 发送 gcm 的 reg_id 数组

mysql - HikariCP 和 MySQL 版本

mysql - 检查用户是否已经注册

java - MySQL CREATE DATABASE IF NOT EXISTS 命令行参数

php - 需要正确的 SQL 查询

mysql - 如何创建命名数据库主机

MySQL 不为 <= 运算符返回正确的数据

PHP-MySQL : Synchronize two calendars

mysql - 使用 php 循环更新 MySQL 表