SQLite加入不起作用

时间:2014-06-06 23:00:35

标签: sql sqlite

我有一个SQL选择了一些行,我希望它也选择行 跟随 我选择的每一行,我无法弄清楚如何编写这样的查询...

简单选择:

SELECT myIndex, scr, date from history where x.state = "Move to Test"

我的最新尝试:

SELECT myIndex, scr, date
from history as x cross join history as y on y.myIndex + 1 = x.myIndex
where state = "Move to Test"

这给出了:"模糊的列名:myIndex"

如果我要像这样指定列表;

SELECT x.myIndex, x.scr, x.date
from history as x cross join history as y on y.myIndex + 1 = x.myIndex
where x.state = "Move to Test"

我从“' y”中选择了什么都没有。表...

以下是我的输出应该的示例:

391 200024   2006-11-27 16:03:43     Move to Test     < I am selecting this row
392 200024   2006-11-27 16:21:02     Dev              < I want to also select the *next* row, regardless of what's in it
395 200024   2006-11-29 10:58:17     Move to Test     < selected
396 200024   2006-11-29 12:10:29     Peer Review      < next
402 200024   2006-11-30 07:27:51     Move to Test     < selected
403 200024   2006-11-30 08:26:13     TQA              < next
408 200024   2007-05-03 08:35:40     Move to Test     < selected
409 200024   2007-05-03 08:52:53     Dev              < next

3 个答案:

答案 0 :(得分:1)

只需将or y.state = "Move to Test"添加到上一个查询中,然后将+更改为-

答案 1 :(得分:1)

在SQLite 3.8.3或更高版本中:

WITH indexes AS (SELECT myIndex
                 FROM history
                 WHERE state = 'Move to Test')
SELECT myIndex,
       scr,
       date
FROM history
WHERE myIndex     IN indexes
   OR myIndex - 1 IN indexes

在早期版本中,您无法使用CTE:

SELECT myIndex,
       scr,
       date
FROM history
WHERE myIndex     IN (SELECT myIndex FROM history WHERE state = 'Move to Test')
   OR myIndex - 1 IN (SELECT myIndex FROM history WHERE state = 'Move to Test')

答案 2 :(得分:0)

如果我理解正确,您希望从两个表中myIndex,那么为什么不使用:

SELECT x.myIndex,y.myIndex x.scr, x.date
from history as x cross join history as y on y.myIndex + 1 = x.myIndex
where x.state = "Move to Test"
相关问题