【问题标题】:MYSQL: Query not properly accounting for max datetime columnMYSQL:查询未正确考虑最大日期时间列
【发布时间】:2018-01-09 17:46:38
【问题描述】:

我有一张桌子 table1 a_id PK, ipaddress, create_dt 这里ipaddressvarcharcreate_dtdatetime

a_id    ip              create_dt
9205    10.10.10.10     2017-01-07 08:03:32
9206    10.10.10.11     2017-01-06 08:03:32
9207    10.10.10.12     2015-01-07 08:03:32

---超过1000行

我有另一个 mysql 表,其以下列 idPKipcheck_typecheck_statusa_idcreated_dt: 这里a_id 是来自table1 的外键 而timestamp_valTIMESTAMP 字段

id      ip             check_type    check_status  a_id    timestamp_val
1       10.10.10.10    check1        FAIL          9205    2017-01-07 10:03:32
2       10.10.10.10    check2        PASS          9205    2017-01-07 10:03:32
3       10.10.10.10    check1        FAIL          9205    2016-11-07 10:03:32
4       10.10.10.10    check2        PASS          9205    2016-11-07 10:03:32
5       10.10.10.11    check1        PASS          9206    2017-01-06 10:03:32
6       10.10.10.11    check2        PASS          9206    2015-01-06 10:03:32

我想要来自table1 的所有行,其中date(create_dt) >= '2017-01-07'table1.a_id = table2.a_idtable2.check1 = 'FAIL' 另外,I only want to consider the row from table2 和最新的timestamp_val

所以从上面的例子中,我的查询应该返回

a_id    ip              create_dt
9205    10.10.10.10     2017-01-07 08:03:32

我已经编写了以下查询,但它并不总是考虑 table2 中具有 max 'timestamp_val'`的行`

我在某些行中也看到了旧的 'timestamp_val'` 的值。

SELECT *  
FROM table1 a INNER JOIN
     table2 b
     ON a.a_id = b.a_id
WHERE a.create_dt >= '2017-01-07' AND
      b.check_status = 'FAIL' AND
      b.check_type = 'check1' AND
      b.timestamp_val = (SELECT MAX(b2.timestamp_val)
                         FROM table2 b2
                         WHERE b2.a_id = b.a_id AND
                               b2.check_status = b.check_status AND
                               b2.check_type = b.check_type
                        );

【问题讨论】:

标签: mysql sql select join inner-join


【解决方案1】:

尝试根据您需要的列使用 group by 以获得最大时间并加入此结果

SELECT *  
FROM table1 a INNER JOIN
     table2 b
     ON a.a_id = b.a_id
INNER JOIN (
  SELECT b2.a_id, b2.check_status, b2.check_type, MAX(b2.timestamp_val) as max_time
  FROM table2 b2
  GROUP BY b2.a_id ,  b2.check_status ,  b2.check_type
  ) t on t.a_id = a.a_id  
      AND  t.check_status=b2.check_status 
      AND t.check_type =b2.check_type
      AND b.timestamp_val = t.max_time
WHERE a.create_dt >= '2017-01-07' AND
      b.check_status = 'FAIL' AND
      b.check_type = 'check1'

【讨论】:

  • 我收到一个错误Unknown column 'b2.check_status' in 'on clause'
  • 应该是AND b.check_status=t.check_status AND b.check_type =t.check_type
  • 我刚刚检查了 timestamp_val 是 TIMESTAMP 字段它不是 datetime 字段。还更新了我的问题
【解决方案2】:

将您的 timestamp_val 更改为 DATETIME(6) - 它将显示毫秒。然后,您将能够正确地按时间排序。 https://dev.mysql.com/doc/refman/5.7/en/fractional-seconds.html

【讨论】:

    【解决方案3】:

    试试这个子查询,现在使用 id 而不是时间戳值:

    b.id = (
        SELECT b2.id
        FROM table2 b2
        WHERE
            b2.a_id = b.a_id
            AND b2.check_status = b.check_status
            AND b2.check_type = b.check_type
        ORDER BY b2.timestamp_val desc limit 1
    )
    

    【讨论】:

    • 我得到了相同的结果。在某些行中仍然可以看到旧的 'timestamp_val'` 的值。
    • timestamp_val 是日期时间字段吗?
    • 我刚刚检查了timestamp_val is TIMESTAMP field 它不是datetime 字段。还更新了我的问题
    • b.id = ( SELECT b2.id FROM table2 b2 WHERE b2.a_id = b.a_id AND b2.check_status = b.check_status AND b2.check_type = b.check_type ORDER BY b2.timestamp_val desc限制 1)
    • b.id 是指b.timestamp_val 吗?我可以要求你更新你的答案更清楚吗?非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多