1

I have a mysql table that has a build number that is formatted like this

client-yearmonthdayhourminute
TKSUS-201310210353

I want to build a query that will pull me the last 4 days. We may have a multiple runs a day so I would want the latest one for the day.

TKSUS-201310210353
TKSUS-201310210153
TKSUS-201310200353
TKSUS-201310190353
TKSUS-201310180353

I tried something like the following but if there is a multiple runs a day it breaks

SELECT DISTINCT build_number FROM test_case_executions WHERE build_number LIKE 'TKSUS%' AND build_number IS NOT NULL ORDER BY build_number DESC LIMIT 4;

Even tried something like this

SELECT DISTINCT SUBSTR(build_number, 7, LENGTH(build_number)-6) FROM test_case_executions WHERE build_number LIKE 'TKSUS%' ORDER BY build_number DESC LIMIT 4;

Thanks for any help.

4

3 回答 3

0

尝试这样的事情......

SELECT DISTINCT build_number
FROM test_case_executions
WHERE
    build_number LIKE 'TKSUS%'
    AND build_number IS NOT NULL
    --this clause gives you only records with date-stamps in the past 4 days
    AND DATE(SUBSTRING(build_number, 7, 9)) >= DATESUB(CURDATE(), INTERVAL 4 DAY)
ORDER BY
    --these two order by clauses will sort by the date descending, and the time ascending
    SUBSTRING(build_number, 7, 9) DESC,
    SUBSTRING(build_number, 15, 4) ASC
;
于 2013-10-21T22:16:45.527 回答
0

您应该能够提取年-月-日部分并GROUP BY在该列上使用,然后MAX(build_number)在该组中选择:

SELECT
  SUBSTR(build_number, 7, 8) AS day,
  MAX(build_number) AS most_recent_build,
  COUNT(build_number) AS num_builds
FROM test_case_executions
  WHERE build_number LIKE 'TKSUS-%'
  GROUP BY day
  ORDER BY day DESC
  LIMIT 4;
于 2013-10-21T22:11:03.760 回答
0

我想我会考虑将 build_number 字段拆分为单独的客户端和日期字段,但这应该可行:

SELECT DISTINCT build_number FROM test_case_executions
  WHERE build_number LIKE CONCAT('TKSUS-',REPLACE(CURRENT_DATE - INTERVAL 4 DAY,'-',''),'%')
ORDER BY build_number DESC;
于 2013-10-21T22:15:15.937 回答