简体   繁体   中英

MYSQL Select Min(Time) where date col is min(Date)

I'm trying to select the Min(Time) where the Date is Min(Date) can someone point me in the right direction?

something along these lines but this doesn't work.

select Min(timecol) as MinTime from myTable where daycol = '1' and datecol = min(datecol)

daycol     datecol         timecol
   1     16/01/2019        08:30:00
   1     17/01/2019        01:30:00
   1     16/01/2019        12:30:00
   1     18/01/2019        12:30:00
   2     16/01/2019        08:30:00

你可以试试:

select timecol as MinTime from myTable where daycol = '1' order by datecol ASC limit 1

This should do it:

SELECT
    MIN( timecol ) AS MinTime
FROM myTable
WHERE daycol = '1'
AND datecol = (
    SELECT
        MIN( datecol )
    FROM myTable
    WHERE daycol = '1'
)
;

If you are running MySQL 8 or later (or any db supporting window functions) then:

SELECT
    daycol, datemin, timemin
FROM (
    SELECT
        *
      , MIN( datecol ) OVER (PARTITION BY daycol) datemin
      , MIN( timecol ) OVER (PARTITION BY daycol, datecol) timemin
    FROM myTable
) d
WHERE daycol = '1'
AND datecol = datemin
AND timecol = timemin

This gives you the Min of timecol where datecol is minimal. select Min(timecol) as MinTime from myTable where datecol = ( select min(datecol) from myTable);

Make sure that timecol and datecol are of types DATE and TIME respectively.

SELECT min(a.timecol), a.datecol, a.daycol FROM `timetable` a 
  WHERE a.datecol = (
      SELECT min(b.datecol) from `timetable` b
  )

This gets the 08:30 on 16/01/2019 row returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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