meaning the result should be selecting 3rd, 7th, 11th, 15 rows etc. Every row has an ID, in ascending order. I am stuck on this for hours! Any help is highly appreciated!
Suppose your schema is:
CREATE TABLE t1 (
id int(11) DEFAULT NULL,
data varchar(20) DEFAULT NULL
);
mysql> select * from t1;
+------+--------+
| id | data |
+------+--------+
| 1 | abc-1 |
| 2 | abc-2 |
| 3 | abc-3 |
| 4 | abc-4 |
| 5 | abc-5 |
| 6 | abc-6 |
| 7 | abc-7 |
| 8 | abc-8 |
| 9 | abc-9 |
| 10 | abc-10 |
| 11 | abc-11 |
| 12 | abc-12 |
| 13 | abc-13 |
| 14 | abc-14 |
| 15 | abc-15 |
| 16 | abc-16 |
+------+--------+
16 rows in set (0.00 sec)
mysql> select id,data from (select mod(@r:=@r+1,4) as isfetch,id,data from t1,(select @r:=0) s) k where k.isfetch=0 order by id;
+------+--------+
| id | data |
+------+--------+
| 4 | abc-4 |
| 8 | abc-8 |
| 12 | abc-12 |
| 16 | abc-16 |
+------+--------+
4 rows in set (0.01 sec)
一种更简单的方法是在 id 本身上使用 mod:
select * from table where (id + 1) mod 4 = 0;
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.