简体   繁体   English

如何合并两个 sql 查询以得到 mysql 中的一张表?

[英]How to combine two sql queries to get a one table in mysql?

I have a dataset as follows,我有一个数据集如下,

   Time         Number  Updated
2/10/2022 10:12 12345     0    
2/11/2022 10:12 234       0    
2/12/2022 10:12 433       0    
2/13/2022 10:12 556       0    
2/14/2022 10:12 4357      0    
2/15/2022 10:12 12345     1 

I need to get Time,Number and Last updated time if updated value is 1 for each number.如果每个数字的更新值为 1,我需要获取时间、数字和上次更新时间。 The output is should be as this. output 应该是这样的。

Time            Number  Updated    Last Updated
2/10/2022 10:12 12345     0      2/15/2022 10:12
2/11/2022 10:12 234       0      2/11/2022 10:12
2/12/2022 10:12 433       0      2/12/2022 10:12
2/13/2022 10:12 556       0      2/13/2022 10:12
2/14/2022 10:12 4357      0      2/14/2022 10:12
2/15/2022 10:12 12345     1      2/15/2022 10:12

I know I need to get following two query data, but I couldn't combine them to get a one query.我知道我需要获取以下两个查询数据,但我无法将它们组合起来以获得一个查询。

select Time,Number from table1;

select Number, max(Time) from table1 group by Number ;

From each one I can get the output as follows,从每个我可以得到 output 如下,

Time            Number  
2/10/2022 10:12 12345   
2/11/2022 10:12 234     
2/12/2022 10:12 433     
2/13/2022 10:12 556     
2/14/2022 10:12 4357    
2/15/2022 10:12 12345


    Number  max(Time)
12345   2/15/2022 10:12
234     2/11/2022 10:12
433     2/12/2022 10:12
556     2/13/2022 10:12
4357    2/14/2022 10:12
12345   2/15/2022 10:12

can some one show me an efficient way to do this?Since I have thousands of queries to select as this.有人可以告诉我一种有效的方法吗?因为我有成千上万的 select 查询。

Update:更新:

How I could get the required outputs when the database changed as below?当数据库发生如下变化时,我如何获得所需的输出?

   Time         Number  Updated
2/10/2022 10:12 12345     1    
2/11/2022 10:12 234       0    
2/12/2022 10:12 433       0    
2/13/2022 10:12 556       0    
2/14/2022 10:12 4357      0    
2/15/2022 10:12 12345     0

and the results should be as follows,结果应如下所示,

Time            Number  Updated    Last Updated
2/10/2022 10:12 12345     1      2/15/2022 10:12
2/11/2022 10:12 234       0      2/11/2022 10:12
2/12/2022 10:12 433       0      2/12/2022 10:12
2/13/2022 10:12 556       0      2/13/2022 10:12
2/14/2022 10:12 4357      0      2/14/2022 10:12
2/15/2022 10:12 12345     0      2/15/2022 10:12

We can use MAX() here as an analytic function:我们可以在这里使用MAX()作为解析 function:

SELECT
    Time,
    Number,
    MAX(Time) OVER (PARTITION BY Number) AS LastUpdated
FROM yourTable
ORDER BY Time;

The logic above will grab, for each record, the max time value as the last updated time.上面的逻辑将为每条记录获取最大时间值作为最后更新时间。 For numbers having only one record, the record's time will be coincident with the max time.对于只有一条记录的号码,记录的时间将与最大时间重合。

Here is a version which should work on MySQL servers earlier than 8+:这是一个应该在早于 8+ 的 MySQL 服务器上工作的版本:

SELECT
    t1.Time,
    t1.Number,
    t2.LastUpdated
FROM yourTable t1
INNER JOIN
(
    SELECT Number, MAX(Time) AS LastUpdated
    FROM yourTable
    GROUP BY Number
) t2
    ON t2.Number = t1.Number
ORDER BY
    t1.Time;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM