简体   繁体   中英

select 2nd row of every ID in mysql

I have a table :

ID | time
1 | 300
1 | 100
1 | 200
2 | 200
2 | 500

I want to get 2nd row for every ID
I know that I can get 1st row as

select ID,time from T group by ID;  

But I don't know about how to get 2nd row for every ID.
I know about limit and offset clause in mysql , but can't figure out how to use them here.
How can I do it ?

EDIT : Actually, time is not ordered. I forgot to specify that. I have made an edit in the table.

i have just an idee how to make it but i couldnt fix it , maybe you can fix it. any suggest is appreciated to correct my query

first this to select the first row of each id.

                  SELECT min(id) id 
                 FROM TableName t2
                 group by id

then select the min(id) which are not in the first query to select to min(id) (which is second row)

like that

      SELECT  min(id) id ,time
      FROM TableName 
      WHERE id NOT IN    (
                          SELECT min(id) id 
                          FROM TableName 
                          GROUP BY id
                         )
      GROUP BY id

** as i said its just suggest . it returns me 0 values.if u fix it let me edit my post to be helpful

here a demo

SELECT ID, MAX(time) time
FROM
    (
        select ID, Time
        from    TableName a
        where 
        (
            select count(*) 
            from TableName as f
            where f.ID = a.ID and f.time <= a.time
        ) <= 2
    ) s
GROUP BY ID
SELECT x.* 
  FROM test x 
  JOIN test y 
    ON y.id = x.id 
   AND y.time >= x.time
 GROUP 
    BY id,time 
HAVING COUNT(*) = n;

Note that any entries with less than n results will be omitted

You cannot do this with the tables that you have. You could make a valiant attempt with:

select id, time
from (select id, time
      from t
      group by t
     ) t
where not exists (select 1 from t t2 where t2.id = t.id and t2.time = t.time)
group by id

That is, attempt to filter out the first row.

The reason this is not possible is because tables are inherently unordered, so there is not real definition of "second" in your tables. This gives the SQL engine the opportunity to rearrange the rows as it sees fit during processing -- which can result in great performance gains.

Even the construct that you are using:

select id, time
from t
group by id

is not guaranteed to return time from the first row . This is a (mis)feature of MySQL called Hidden Columns. It is really only intended for the case where all the values are the same. I will admit that in practice it seems to get the value from the first row, but you cannot guarantee that.

Probably your best solution is to select the data into a new table that has an auto-incrementing column:

create table newtable (
    autoid int auto_increment,
    id int,
    time int
);

insert into newtable(id, time)
    select id, time from t;

In practice, this will probably keep the same order as the original table, and you can then use the autoid to get the second row. I want to emphasize, though, the "in practice". There is no guarantee that the values are in the correct order, but they probably will be.

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