【问题标题】:MySQL : select 3 column names with greatest values from a rowMySQL:从一行中选择 3 个具有最大值的列名
【发布时间】:2015-09-25 14:25:53
【问题描述】:

我是 MySQL 新手,遇到以下问题:

我需要选择 3 个具有最大值的列名,而我的“国家”表的结构如下:

countryCode   tag1   tag2   tag3   tag4   tag5   tag6   tag7   tag8

     GB        2      4      4       8      9      1      1     0 
     .
     .
     .

所以在这种情况下查询应该返回 tag5, tag4, tag3。 我搞砸了 select max 和 union,但还没有成功。

任何帮助表示赞赏:)

进一步说明

countryCode backpack beach culture forest mountains scuba trailer
   AD          0       5      85     1       5        6      7  
   AE          7       2      15     1       9        6      7 
   .
   .
   .

我从 PHP 查询,将 countryCode 作为 Post。我需要得到具有最高 3 个数字的列的结果。对于这个例子;如果我查询 AD,它应该返回文化、预告片、水肺

【问题讨论】:

  • 预期输出是什么?只需将其添加到问题中即可。
  • 相信你忽略了,这个case应该返回tag5,tag4,tag3
  • 只需要返回 column_name 对吗?
  • 是的,没错,只要我得到列名就足够了,最好是按某种顺序,asc 或 desc :)
  • 见规范化。数据库表不是电子表格

标签: php mysql filter multiple-columns


【解决方案1】:

我不确定我是否完全理解你的问题,所以我添加了一点 - 假设我有这个输入:

countryCode   tag1   tag2   tag3   tag4   tag5   tag6   tag7   tag8

     GB        2      4      5       8      9      1      1     0 
     FR        9      4      4       8      9      1      7     0 
     .
     .

然后您希望输出为: 国家代码 number1 number2 number3

     GB        tag5           tag4      tag3       
     FR        tag1, tag5     tag4      tag7

?

如果您期望上述结果,max 将无济于事,您的数据格式也无济于事。你可能会做类似下面的事情,这很糟糕......

select countryCode, tagName, value from 
(select a.countryCode, 'tag1'  tagName, a.tag1  value from countries a where a.countryCode = 'GB' UNION
select  b.countryCode, 'tag2'  tagName, b.tag2  value from countries b where b.countryCode = 'GB' UNION
select  c.countryCode, 'tag3'  tagName, c.tag3  value from countries c where c.countryCode = 'GB' UNION
select  d.countryCode, 'tag4'  tagName, d.tag4  value from countries d where d.countryCode = 'GB' UNION
select  e.countryCode, 'tag5'  tagName, e.tag5  value from countries e where e.countryCode = 'GB' UNION
select  f.countryCode, 'tag6'  tagName, f.tag6  value from countries f where f.countryCode = 'GB' UNION
select  g.countryCode, 'tag7'  tagName, g.tag7  value from countries g where g.countryCode = 'GB' UNION
select  h.countryCode, 'tag8'  tagName, h.tag8  value from countries h where h.countryCode = 'GB' UNION
select  i.countryCode, 'tag9'  tagName, i.tag9  value from countries i where i.countryCode = 'GB' UNION
select  j.countryCode, 'tag10' tagName, j.tag10 value from countries j where j.countryCode = 'GB' ) 
as results
order by value desc
limit 3;

编辑:只是我所拥有的数据和表结构的一个示例。我在 MySQL 5.5.44-0ubuntu0.12.04.1 中运行它,以防万一这是版本差异。因此,如果您调整我上面的示例查询,那应该可以工作。

mysql> desc countries;
+-------------+------------+------+-----+---------+-------+
| Field       | Type       | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+-------+
| countryCode | varchar(2) | YES  |     | NULL    |       |
| tag1        | int(11)    | YES  |     | NULL    |       |
| tag2        | int(11)    | YES  |     | NULL    |       |
| tag3        | int(11)    | YES  |     | NULL    |       |
| tag4        | int(11)    | YES  |     | NULL    |       |
| tag5        | int(11)    | YES  |     | NULL    |       |
| tag6        | int(11)    | YES  |     | NULL    |       |
| tag7        | int(11)    | YES  |     | NULL    |       |
| tag8        | int(11)    | YES  |     | NULL    |       |
| tag9        | int(11)    | YES  |     | NULL    |       |
| tag10       | int(11)    | YES  |     | NULL    |       |
+-------------+------------+------+-----+---------+-------+
11 rows in set (0.00 sec)

mysql> select * from countries;
+-------------+------+------+------+------+------+------+------+------+------+-------+
| countryCode | tag1 | tag2 | tag3 | tag4 | tag5 | tag6 | tag7 | tag8 | tag9 | tag10 |
+-------------+------+------+------+------+------+------+------+------+------+-------+
| GB          |    5 |    1 |    2 |    5 |    7 |    8 |    9 |    3 |   15 |    22 |
| FR          |    5 |   14 |    2 |    5 |    7 |    8 |    9 |    3 |   15 |    22 |
+-------------+------+------+------+------+------+------+------+------+------+-------+
2 rows in set (0.00 sec)

但理想情况下,如果您在其他地方获取此数据,则可能更容易且更推荐使用其他表,因为我发送给您的查询会很慢。

理想的情况是您有一个表 Country Code and Tag,您可以直接在其上使用聚合函数。

【讨论】:

  • 我似乎无法运行此查询,如果我这样做会返回语法错误:select countryCode, tagName, value from (select a.countryCode, 'beach' tagName, a.beach value从国家 a 其中 a.countryCode = 'AD' UNION 选择 b.countryCode, 'backpack' tagName, b.backpack 值 从国家 b 其中 b.countryCode = 'AD' UNION 选择 c.countryCode, 'scuba' tagName, c。来自国家 c 的 scuba 值,其中 c.countryCode = 'AD' UNION ) 作为结果顺序按值 desc 限制 1;
  • 我已经添加了一个数据样本和我的表结构——所以如果你有类似的东西,那个查询应该可以工作。我会说,如果您需要这个蓝月亮脚本并且性能要求较低,那么您可以使用该查询 - 否则,我会说您需要以不同的格式找到这些数据,就像这样例如 country_code | varchar(2) 标签名 | varchar(128) 标签计数 |整数(11)
  • 感谢您的努力。已经确信要正确设计它:) 我将创建一个包含国家代码、标签和值的附加表。无法投票,因为我没有足够的声望点。
  • 顺便说一句,我刚刚注意到您在上面的评论中的查询中有一个额外的 UNION:select countryCode, tagName, value from (select a.countryCode, 'beach' tagName, a.beach value从国家 a 其中 a.countryCode = 'AD' UNION 选择 b.countryCode, 'backpack' tagName, b.backpack 值 从国家 b 其中 b.countryCode = 'AD' UNION 选择 c.countryCode, 'scuba' tagName, c。来自国家 c 的 scuba 值,其中 c.countryCode = 'AD') 作为结果顺序,按值 desc 限制 1;
猜你喜欢
  • 2015-01-27
  • 2018-12-16
  • 2021-07-23
  • 2018-10-06
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-03
相关资源
最近更新 更多