mysql - 如何使用矩阵顺序选择顶部不同的值

标签 mysql sql

我有一张这样的 table

enter image description here

我想获取每个表中具有最短响应时间的行

我尝试过这个查询:

select tablename,
       index1, 
       index2, 
       min(responsetime) 
from   tableconf 
group by tablename 
order by responsetime asc

但它没有给出我想要的

我想要的输出是

+------------------+------------------+--------+--------------+
|    tablename     |     index1       | index2 | responsetime |
+------------------+------------------+--------+--------------+
| salesorderheader | TotalDue         | NULL   |       6.1555 |
| salesterritory   | Name             | NULL   |     11.66667 |
| store            | BusinessEntityId | Name   |       3.6222 |
| previous         | previous         | NULL   |      5.03333 |
| NONE             | NONE             | NULL   |          5.6 |
+------------------+------------------+--------+--------------+

我应该使用什么查询来获取我想要的输出

最佳答案

选择每个表名称的最短日期。对这些使用 IN 子句来获取行:

select *
from tableconf 
where (tablename, responsetime) in
(
  select tablename, min(responsetime)
  from tableconf 
  group by tablename
);

关于mysql - 如何使用矩阵顺序选择顶部不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44417225/

相关文章:

MySQL 多个左连接在一张表上

java - 如何避免将空值发送到java中的sql中

mysql - 仅显示表中 MySQL 中列的名称

MySQL SUM 大 TIMEDIFF

Java 数据库 MySQL

php - MySQL 和 PHP 时区

java - 使用 Hibernate 时未在多个事务中加载 Childs

mysql - 搜索三列

sql - 这个MySQL语句: DECLARE @ID INT有什么问题

php - 从 select 语句 (MySQL) 中的多个日期列中删除时间戳