mysql - 需要帮助编写查询(重组表)
问题描述
我需要编写一个选择语句,它将以下列方式重写表......我不确定如何使用 MySQL 来解决这个问题。
表格示例
user_id date a b c
123456 2020-01-01 1 1 1
234567 2020-03-04 1 0 0
453576 2020-05-05 1 0 1
期望的结果
user_id date results
123456 2020-01-01 a
123456 2020-01-01 b
123456 2020-01-01 c
234567 2020-03-04 a
453576 2020-05-05 a
453576 2020-05-05 c
解决方案
在 MySQL 中,您可以使用union all
, 同时过滤1
值:
select user_id, date, 'a' as result from mytable where a = 1
union all select user_id, date, 'b' from mytable where b = 1
union all select user_id, date, 'c' from mytable where c = 1
order by user_id, date, result
推荐阅读
- spring-boot - oauth2-oidc-sdk ParseException:“不接受无算法”
- javascript - 如何阻止我的团队在 React 中使用原始字符串作为 JSX 子项?
- regex - 在记事本++中以n个字符完成整个单词的拆分短语
- sql - MariaDB:COUNT DISTINCT 是否被窃听?
- c# - 使用方法转义 XML 时出现 System.OutOfMemoryException
- java - 使用 jsoup 解析 londonstockexchange 页面
- java - 如何找到价格的平均值?
- css - 将导航栏定位在页面顶部但在主要内容上失去可点击性
- ruby - 查找一个项目是否在数组中的另一个项目之前
- spring - Spring Boot BindingResult 不显示任何错误消息