MYSQL根据特定列选择多个列

MYSQL根据特定列选择多个列,第1张

概述我想做一个SELECT请求,根据PLATFORM值,设法获得2列VALUE(DESKTOP& MOBILE).这是一个示例表:+----+---------+------+----------+-------+ | ID | PROJECT | NAME | PLATFORM | VALUE | +----+---------+------+----

我想做一个SELECT请求,根据PLATFORM值,设法获得2列VALUE(DESKtop& MOBILE).

这是一个示例表:

+----+---------+------+----------+-------+| ID | PROJECT | name | PLATFORM | VALUE |+----+---------+------+----------+-------+|  1 |       1 | Foo  | desktop  |     1 ||  2 |       1 | Foo  | mobile   |    42 ||  3 |       1 | bar  | desktop  |     3 ||  4 |       1 | bar  | mobile   |    10 ||  5 |       2 | Foo  | desktop  |     2 ||  6 |       2 | bar  | mobile   |     9 |+----+---------+------+----------+-------+

期望的输出:

+---------+------+---------+--------+| PROJECT | name | DESKtop | MOBILE |+---------+------+---------+--------+|       1 | Foo  | 1       | 42     ||       1 | bar  | 3       | 10     ||       2 | Foo  | 2       | NulL   ||       2 | bar  | NulL    | 9      |+---------+------+---------+--------+

我尝试了什么:

SELECT project,name,(CASE platform WHEN 'desktop' THEN value END) AS "desktop",(CASE platform WHEN 'mobile' THEN value END) AS "mobile"FROM testGROUP BY name,projectORDER BY project,value ASC+---------+------+---------+--------+| project | name | desktop | mobile |+---------+------+---------+--------+|       1 | Foo  | 1       | NulL   ||       1 | bar  | 3       | NulL   ||       2 | Foo  | 2       | NulL   ||       2 | bar  | NulL    | 9      |+---------+------+---------+--------+
最佳答案试试这个:

SELECT project,name,MAX(desktop) AS desktop,MAX(mobile) AS mobile FROM (    SELECT project,(CASE platform WHEN 'desktop' THEN VALUE END) AS "desktop",(CASE platform WHEN 'mobile' THEN VALUE END) AS "mobile"    FROM test    ) AS aaGROUP BY aa.name,aa.projectORDER BY aa.project

说明:

首先,您可以选择(aa)所有数据,根据平台内容扩展价值.

然后使用该选择作为分组数据的原点.

结果:

project name   desktop mobile1       Foo       1       421       bar       3       102       Foo       2       NulL2       bar       NulL    9
总结

以上是内存溢出为你收集整理的MYSQL根据特定列选择多个列全部内容,希望文章能够帮你解决MYSQL根据特定列选择多个列所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1168696.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存