从一系列表条目中返回第二行

时间:2013-08-06 17:45:05

标签: mysql

我正在尝试生成仅返回系列条目的第二行的报告。

当我使用limit / offset时,我的返回只是一行,应该是。这不是我想要的。

这就是我所拥有的:

SELECT HD_TICKET_ID, 
HD_TICKET.CREATED, 
HD_TICKET_CHANGE.TIMESTAMP,
USER.FULL_NAME

FROM `ORG1`.`HD_TICKET_CHANGE`, 
`ORG1`.`USER`, 
`ORG1`.`HD_TICKET`
WHERE (HD_TICKET_ID = HD_TICKET.ID)

AND HD_TICKET_CHANGE.USER_ID = USER.ID 

ORDER BY  HD_TICKET_ID

返回

HD_TICKET_ID,Created,Timestamp,Full_Name
....
31  2008-04-15 17:26:25 2008-04-15 17:26:25 S. Redacted
31  2008-04-15 17:26:25 2008-04-16 11:48:34 C. redacted
31  2008-04-15 17:26:25 2008-04-16 12:18:25 C. redacted
31  2008-04-15 17:26:25 2008-04-17 15:19:34 M. redacted
33  2008-04-17 06:23:27 2008-04-17 08:40:24 A. redacted
33  2008-04-17 06:23:27 2008-04-17 09:18:17 C. redacted
33  2008-04-17 06:23:27 2008-04-17 08:35:41 C. redacted
33  2008-04-17 06:23:27 2008-04-17 08:30:00 C. redacted
33  2008-04-17 06:23:27 2008-04-17 06:23:27 L. redacted
33  2008-04-17 06:23:27 2008-04-17 15:18:55 M. redacted
36  2008-04-17 07:37:20 2008-04-17 11:31:19 M. redacted
36  2008-04-17 07:37:20 2008-04-17 07:37:20 A. redacted
36  2008-04-17 07:37:20 2008-04-17 07:48:58 A. redacted
36  2008-04-17 07:37:20 2008-04-17 07:56:34 A. redacted
36  2008-04-17 07:37:20 2008-04-17 08:04:36 A. redacted
42  2008-04-17 09:11:28 2008-04-17 09:36:14 M. redacted
42  2008-04-17 09:11:28 2008-04-17 09:11:28 S. Redacted
42  2008-04-17 09:11:28 2008-04-23 00:03:20 M. redacted
42  2008-04-17 09:11:28 2008-04-24 13:06:18 S. Redacted
42  2008-04-17 09:11:28 2008-04-24 13:06:31 S. Redacted
....

然而,我想要的只是每个人的第二行;所以输出应该是这样的:

....
31  2008-04-15 17:26:25 2008-04-16 11:48:34 C. redacted
33  2008-04-17 06:23:27 2008-04-17 09:18:17 C. redacted
36  2008-04-17 07:37:20 2008-04-17 07:37:20 A. redacted
42  2008-04-17 09:11:28 2008-04-17 09:11:28 S. Redacted
....

1 个答案:

答案 0 :(得分:0)

您可以使用会话变量,以下是示例代码

 select * from 
      (select @rowid:=@rowid+1 as rowid, 
                id, username from user, 
                (select @rowid:=0)as init) as t 
      where rowid%2=0;

至于你的目的,我想你可以编写你的sql如下

SELECT * FROM
    (SELECT @rowid:=@rowid+1 as rowid, 
        HD_TICKET_ID, 
        HD_TICKET.CREATED, 
        HD_TICKET_CHANGE.TIMESTAMP,
        USER.FULL_NAME

    FROM `ORG1`.`HD_TICKET_CHANGE`, 
        `ORG1`.`USER`, 
        `ORG1`.`HD_TICKET`,
        (SELECT @rowid:=0 as init) as rowtable
    WHERE (HD_TICKET_ID = HD_TICKET.ID)
        AND HD_TICKET_CHANGE.USER_ID = USER.ID
    ORDER BY  HD_TICKET_ID) as t
WHERE rowid%2=0
希望有所帮助!