Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何选择该日期内的日期和时间?_Mysql - Fatal编程技术网

Mysql 如何选择该日期内的日期和时间?

Mysql 如何选择该日期内的日期和时间?,mysql,Mysql,我读了几篇关于这方面的文章:这篇文章似乎最有帮助,但我看不到实现它的方法 有五张桌子。我加入他们。我只需要从第一个表中选择一个日期和时间最高的行,从第二个表中选择相同的行,并在其他值上连接其余的行。使用我编写的代码,我得到了多行。看来时间的选择是不对的 这可以通过子查询中的子查询来完成。我试过这样的方法: SELECT * from table1 INNER JOIN table2 ON table1.date = table2.date AND table1.gm = tab

我读了几篇关于这方面的文章:这篇文章似乎最有帮助,但我看不到实现它的方法

有五张桌子。我加入他们。我只需要从第一个表中选择一个日期和时间最高的行,从第二个表中选择相同的行,并在其他值上连接其余的行。使用我编写的代码,我得到了多行。看来时间的选择是不对的

这可以通过子查询中的子查询来完成。我试过这样的方法:

    SELECT * from table1 
    INNER JOIN table2 ON table1.date = table2.date AND table1.gm = table2.gm        
    INNER JOIN table3 ON table2.gm = table3.gm        ...
    WHERE table3.date = :date AND table4.date = :date ...
    AND table1.date IN(
        SELECT MAX(table1.date) FROM table1 WHERE table1.time IN(
            SELECT MAX(table1.time) FROM table1
    )
    )
    AND table2.date IN(
        SELECT MAX(table2.date) FROM table1 WHERE table2.time IN(
            SELECT MAX(table2.time) FROM table2        )
    )
    ORDER BY table1.id
问题是: 如何在加入所有这些之后获得单行,其中日期最高,时间在该日期最高? 谢谢

编辑:对此我很抱歉。我忘了说我需要与表(gm列)中的特定值相关的max time of max date。这是一行(在我给出的例子中是table1.gm和table2.gm…),每个表中的gm值都是相同的,而不仅仅是一行。解决方案尼克和萨利姆提供了工作,但我并没有解决这个问题

编辑:解决了!在实施Nick的解决方案后,我刚刚需要添加cntrs_repro.gm_company_no,cntrs_repro.date的组。 就这样。对于一个表格中的每一行,其他表格中的日期和时间都是最高的!!谢谢大家

编辑。如果这有助于完成完整查询:

SELECT  cntrs_gm.gm_company_no                          AS      company_c_g,  
 bns_gms.ded_bns                                    AS      ded_bns_gms,  
     bns_gms.no_ded_bns                             AS      no_ded_bns_gms,  
     bns_gms.wag_ded_bns                            AS      wag_ded_bns_gms,  
     cntrs_gm.cur_credit                            AS      cur_credit_c_g,  
     cntrs_gm.cdrop                                 AS      cdrop_c_g,  
     cntrs_gm.total_jp                              AS      total_jp_c_g,  
     cntrs_gm.games                                 AS      games_c_g,  
     cntrs_gm.wgames                                AS      wgames_c_g,  
     cntrs_gm.doors                                 AS      doors_c_g,  
     cntrs_gm.power                                 AS      power_c_g,  
     cntrs_gm.total_in                            AS        total_in_c_g,  
     cntrs_gm.total_out                           AS        total_out_c_g,  
     cntrs_gm.total_acc                           AS        total_acc_c_g,  
     cntrs_gm.total_bet                           AS        total_bet_c_g,  
     cntrs_gm.total_win                           AS        total_win_c_g,  
     cntrs_gm.total_bonus                         AS        total_bonus_c_g,  
     cntrs_gm.date              AS      date_c_g,  
     cntrs_reper.gm_company_no                    AS        company_reper,  
 bns_reper.ded_bns                            AS        ded_bns_reper,  
     bns_reper.no_ded_bns                         AS        no_ded_bns_reper,  
     bns_reper.wag_ded_bns          AS      wag_ded_bns_reper,  
     cntrs_reper.cur_credit         AS      cur_credit_reper,  
     cntrs_reper.cdrop              AS      cdrop_reper,  
     cntrs_reper.total_jp                         AS        total_jp_reper,  
     cntrs_reper.games              AS      games_reper,  
     cntrs_reper.wgames             AS      wgames_reper,  
     cntrs_reper.doors              AS      doors_reper,  
     cntrs_reper.power              AS      power_reper,  
     cntrs_reper.total_in                         AS        total_in_reper,  
     cntrs_reper.total_out          AS      total_out_reper,  
     cntrs_reper.total_acc          AS      total_acc_reper,  
     cntrs_reper.total_bet          AS      total_bet_reper,  
     cntrs_reper.total_win          AS      total_win_reper,  
     cntrs_reper.total_bonus            AS      total_bonus_reper,  
     cntrs_reper.date                             AS        date_reper,  
     cntrs_reper.time               AS      time_reper,
     bns_reper.time                 AS      time_c_g,
     gms_cfg.gm_no                                    AS              machine_id,  
     gms_cfg.denom_cin                        AS              machine_cin  
     FROM bns_gms  
     INNER JOIN cntrs_gm  
     ON bns_gms.gm_company_no = cntrs_gm.gm_company_no  AND bns_gms.date = cntrs_gm.date
     INNER JOIN bns_reper 
     ON cntrs_gm.gm_company_no = bns_reper.gm_company_no  
     INNER JOIN cntrs_reper  
     ON bns_reper.gm_company_no = cntrs_reper.gm_company_no  AND bns_reper.date = cntrs_reper.date
     INNER JOIN gms_cfg  
     ON cntrs_reper.gm_company_no =  gms_cfg.gm_no  
     WHERE   bns_reper.date IN(
        SELECT MAX(DATE(bns_reper.date)) FROM bns_reper WHERE bns_reper.time IN(
            SELECT MAX(TIME(bns_reper.time)) FROM bns_reper
        )
        )
     AND cntrs_reper.date IN(
        SELECT MAX(DATE(cntrs_reper.date)) FROM cntrs_reper  WHERE cntrs_reper.time IN(
            SELECT MAX(TIME(cntrs_reper.time)) FROM cntrs_reper
        )
        )

     ORDER BY cntrs_gm.gm_company_no
数据库示例


我没有MySQL,但这里是您可以使用的总体思路。我没有足够的分数来写评论,所以我只是回复。实际上,为每个表创建一个子查询/内联视图,以选择列的最大值,然后将这些子查询/内联视图连接在一起

下面是Oracle语法。您可以将其转换为ANSI语法

select table1.column1, table2.column2,table3.column3 
from
(select id1, max(column1) as column1 from table1 group by id1) as table1
(select id2, max(column2) as column2 from table2  group by id2) as table2
(select id3, max(column3) as column3 from table3  group by id3) as table3
where 
table1.id1 = table2.id2
and table1.id1 = table3.id3
;

当前查询的问题是,它将选择table1.date是最晚发生时间的日期的所有行,该日期可能不止一个,例如

id  date          time
1   2018-03-30    18:40
2   2018-03-31    12:20
3   2018-03-31    19:20
您的
WHERE
子句:

table1.date IN(
    SELECT MAX(table1.date) FROM table1 WHERE table1.time IN(
        SELECT MAX(table1.time) FROM table1
)
将选择id=2和id=3的行,因为它们都有日期为2018-03-31的最长时间

您要做的是选择在最晚日期上具有最晚时间的行,您可以使用该行

table1.date = (SELECT MAX(date) FROM table1) AND
table1.time = (SELECT MAX(time) FROM table1 WHERE date = (SELECT(MAX(date) FROM table1))
通过使用别名,可以将其简化(因为我们已经知道表1中的
table1.date=MAX(date))为


尝试使用
MAX(日期(表1.DATE))
MAX(时间(表2.DATE))
。第一个将返回最大日期,第二个将返回最大时间。感谢您的回复,但它再次返回多行。您所说的
在其他值上加入其他值是什么意思?是否在同一查询中联接其他表?表1、表2和其他3个表之间的关系是什么?其他3个表没有时间列。他们每天都有随时更新的条目。我加入他们的日期和身份证号码。是的,我在同一个查询中加入了它们。如果需要的话,我会带着问题编辑完整的查询?你能提供你表格中数据的例子吗?测试连接或比较结果将很有帮助。Nick谢谢!我试着实施你的建议。就像你说的那样。我忘了说一件很重要的事。它应该为每个特定列值返回一行。所有表都与samo值相关。它是gms_cfg_gm和*.gm_company_no.在所有其他方面。有39排。我应该得到他们每个人的最长时间和最长日期。所以结果应该包含39行。如果你有任何进一步的建议,我愿意倾听。嗨,佩拉,看起来你自己解决了这个问题?在你的大力帮助下!我找不到(从表1中选择MAX(time)作为t1,其中t1.date=table1.date)解决方案。这就是你的答案被接受的原因。谢谢-我很高兴我能帮助你解决整个问题。谢谢萨利姆。我实施了尼克的解决方案。但你的回答让我想到了另一个办法,那就是分组讨论。我还不能投票,但我会尽快给你+1!
table1.date = (SELECT MAX(date) FROM table1) AND
table1.time = (SELECT MAX(time) FROM table1 AS t1 WHERE t1.date = table1.date)