MySQL查询优化LEFT JOIN和DISTINCT

时间:2019-04-24 08:20:27

标签: mysql sql left-join distinct

我在MySQL中有两个表:

system

Id_system   Name        Type
------------------------------
1           'System1'   'Type1'
2           'System2'   'Type1'
3           'System3'   'Type1'
4           'System4'   'Type2'

measures

Id_system   Sensor  Value   Insert_date
--------------------------------------------------
1           'S1'    12      '2019-23-04 15:16:05'
1           'S2'    2       '2019-23-04 15:16:05'
1           'S3'    42      '2019-23-04 15:16:05'
2           'S1'    11      '2019-23-04 15:11:05'
2           'S2'    3       '2019-23-04 15:11:05'
2           'S3'    43      '2019-23-04 15:11:05'
4           'S1'    2       '2019-23-04 15:10:05'
4           'S2'    32      '2019-23-04 15:10:05'
4           'S3'    2       '2019-23-04 15:10:05'
3           'S1'    32      '2019-23-04 15:06:05'
3           'S2'    54      '2019-23-04 15:06:05'
3           'S3'    15      '2019-23-04 15:06:05'
1           'S1'    10      '2019-23-04 15:00:05'
1           'S2'    1       '2019-23-04 15:00:05'
1           'S3'    35      '2019-23-04 15:00:05'

我希望得到每个系统的最后度量,例如:

Id_system   Sensor  Value   Insert_date             Name        
---------------------------------------------------------------
1           'S1'    12      '2019-23-04 15:16:05'   'System1'
1           'S2'    2       '2019-23-04 15:16:05'   'System1'
1           'S3'    42      '2019-23-04 15:16:05'   'System1'
2           'S1'    11      '2019-23-04 15:11:05'   'System2'
2           'S2'    3       '2019-23-04 15:11:05'   'System2'
2           'S3'    43      '2019-23-04 15:11:05'   'System2'
3           'S1'    32      '2019-23-04 15:06:05'   'System3'
3           'S2'    54      '2019-23-04 15:06:05'   'System3'
3           'S3'    15      '2019-23-04 15:06:05'   'System3'

我在sql中尝试了此请求:

SELECT DISTINCT m.sensor, s.name, m.value, m.date_insert 
FROM system s 
LEFT JOIN measure m ON s.id_system = m.id_system 
WHERE s.type = 'Type1'

但是她回来了,每个系统的每个度量,而我只想要每个系统每种类型的最后一个度量:

Id_system   Sensor  Value   Insert_date             Name        
---------------------------------------------------------------
1           'S1'    12      '2019-23-04 15:16:05'   'System1'
1           'S2'    2       '2019-23-04 15:16:05'   'System1'
1           'S3'    42      '2019-23-04 15:16:05'   'System1'
2           'S1'    11      '2019-23-04 15:11:05'   'System2'
2           'S2'    3       '2019-23-04 15:11:05'   'System2'
2           'S3'    43      '2019-23-04 15:11:05'   'System2'
3           'S1'    32      '2019-23-04 15:06:05'   'System3'
3           'S2'    54      '2019-23-04 15:06:05'   'System3'
3           'S3'    15      '2019-23-04 15:06:05'   'System3'
1           'S1'    10      '2019-23-04 15:00:05'   'System1'
1           'S2'    1       '2019-23-04 15:00:05'   'System1'
1           'S3'    35      '2019-23-04 15:00:05'   'System1'

您是否知道该怎么做?

1 个答案:

答案 0 :(得分:0)

这是典型的每组最大问题

qDebug() << val.leftJustified(20, /*QChar fill=*/QLatin1Char(' '), /*bool truncate=*/false);