Home  >  Q&A  >  body text

The Chinese translation of MySQL MIN() + 1 row is "MySQL minimum value() + 1 row"

I'm running a SELECT query to get data from MySQL

SELECT
    MIN(datetime) as created,
    MAX(datetime) as updated,
    COUNT(CASE WHEN type = 'update' AND contact_name <> 'System' THEN 1 END) as replies,
    COUNT(CASE WHEN type = 'update' AND (contact_name * 1 = contact_name) THEN 1 END) as customer_replies

And it works fine, but I also want to get the next row after MIN(datetime)

Is it possible to do this like MIN() 1?

P粉564301782P粉564301782408 days ago407

reply all(1)I'll reply

  • P粉805535434

    P粉8055354342023-09-08 11:38:19

    Here is a solution that gives the second smallest value:

    SELECT
        MIN(datetime) as created_second_minimum 
    FROM TableName
    ORDER BY datetime
    LIMIT 1,1;

    When we use LIMIT n, it returns the first n rows, and when we use LIMIT n,m, it returns the m rows after the nth row (excluding the nth row). In our case it doesn't return the first row, only the second row. Since we ordered the query by datetime, the second row is the second oldest.

    reply
    0
  • Cancelreply