这是我的桌子。
+----+--------+---------+------------+ | id | idName | fldName | fld_Date | +----+--------+---------+------------+ | 1 | 1 | Marlon | 2013-06-03 | | 2 | 1 | Marlon | 2013-06-05 | | 3 | 1 | Marlon | 2013-06-07 | | 4 | 1 | Marlon | 2013-06-08 | | 5 | 1 | Marlon | 2013-06-11 | | 6 | 2 | Dawn | 2013-06-03 | | 7 | 2 | Dawn | 2013-06-06 | | 8 | 2 | Dawn | 2013-06-08 | | 9 | 2 | Dawn | 2013-06-11 | | 10 | 2 | Dawn | 2013-06-15 | | 11 | 3 | Jenny | 2013-06-15 | | 12 | 3 | Jenny | 2013-06-19 | | 13 | 3 | Jenny | 2013-06-14 | | 14 | 3 | Jenny | 2013-06-21 | +----+--------+---------+------------+
输出应该是这样的
+----+--------+---------+----+------------+------------+ | id | idName | fldName | i | fld_Date | next_date | +----+--------+---------+----+------------+------------+ | 1 | 1 | Marlon | 1 | 2013-06-03 | 2013-06-05 | | 2 | 1 | Marlon | 2 | 2013-06-05 | 2013-06-07 | | 3 | 1 | Marlon | 3 | 2013-06-07 | 2013-06-08 | | 4 | 1 | Marlon | 4 | 2013-06-08 | 2013-06-11 | | 5 | 1 | Marlon | 5 | 2013-06-11 | 0 | | 6 | 2 | Dawn | 1 | 2013-06-03 | 2013-06-06 | | 7 | 2 | Dawn | 2 | 2013-06-06 | 2013-06-08 | | 8 | 2 | Dawn | 3 | 2013-06-08 | 2013-06-11 | | 9 | 2 | Dawn | 4 | 2013-06-11 | 2013-06-15 | | 10 | 2 | Dawn | 5 | 2013-06-15 | 0 | | 11 | 3 | Jenny | 1 | 2013-06-15 | 2013-06-19 | | 12 | 3 | Jenny | 2 | 2013-06-19 | 2013-06-14 | | 13 | 3 | Jenny | 3 | 2013-06-14 | 2013-06-21 | | 14 | 3 | Jenny | 4 | 2013-06-21 | 0 | +----+--------+---------+----+------------+------------+
这是我的代码…
SELECT id,idName,fldName, @i := @i + 1 i, fld_Date, next_date FROM ( SELECT id,idName,fldName, @d next_date, @d := fld_Date fld_Date FROM x_table, (SELECT @d := 0) d ORDER BY id DESC) q, (SELECT @i := 0) n ORDER BY id;
我很难根据不同的数量来限制增量idName…您能帮我这些吗....
idName
试试这个:
SELECT id, idname, fldname, IF(@idname=(@idname:=idname), @id:=@id+1, @id:=1) i, fld_Date, next_Date FROM (SELECT a.id, a.idName, a.fldName, a.fld_Date, IFNULL(b.fld_Date, 0) next_Date FROM x_table a LEFT JOIN x_table b ON a.idname = b.idname AND a.fld_Date < b.fld_Date GROUP BY a.id) A, (SELECT @id:=0, @idname:=0) B
检查 FIDDLE 上的查询 ****
输出
| ID | IDNAME | FLDNAME | I | FLD_DATE | NEXT_DATE | ------------------------------------------------------------------------ | 1 | 1 | Marlon | 1 | June, 03 2013 00:00:00+0000 | 2013-06-05 | | 2 | 1 | Marlon | 2 | June, 05 2013 00:00:00+0000 | 2013-06-07 | | 3 | 1 | Marlon | 3 | June, 07 2013 00:00:00+0000 | 2013-06-08 | | 4 | 1 | Marlon | 4 | June, 08 2013 00:00:00+0000 | 2013-06-11 | | 5 | 1 | Marlon | 5 | June, 11 2013 00:00:00+0000 | 0 | | 6 | 2 | Dawn | 1 | June, 03 2013 00:00:00+0000 | 2013-06-06 | | 7 | 2 | Dawn | 2 | June, 06 2013 00:00:00+0000 | 2013-06-08 | | 8 | 2 | Dawn | 3 | June, 08 2013 00:00:00+0000 | 2013-06-11 | | 9 | 2 | Dawn | 4 | June, 11 2013 00:00:00+0000 | 2013-06-15 | | 10 | 2 | Dawn | 5 | June, 15 2013 00:00:00+0000 | 0 | | 11 | 3 | Jenny | 1 | June, 15 2013 00:00:00+0000 | 2013-06-19 | | 12 | 3 | Jenny | 2 | June, 19 2013 00:00:00+0000 | 2013-06-21 | | 13 | 3 | Jenny | 3 | June, 14 2013 00:00:00+0000 | 2013-06-15 | | 14 | 3 | Jenny | 4 | June, 21 2013 00:00:00+0000 | 0 |