SQLite Forum

Retrieving FIRST occurrence of a string in a column (SQLITE PHP)
Login
- previous occurrence of string "2" (if a previous entry is present)

```
  select progressive_no 
    from MyTable
   where ColumnX == :string
     and progressive_no < :lastno
order by progressive_no desc
   limit 1;
```

- next occurrence of string "2"  (if another entry is present)

```
  select progressive_no 
    from MyTable
   where ColumnX == :string
     and progressive_no > :lastno
order by progressive_no 
   limit 1;
```

- first occurrence of string "2"

```
  select progressive_no 
    from MyTable
   where ColumnX == :string
order by progressive_no 
   limit 1;
```

- last occurrence of string "2" 

```
  select progressive_no 
    from MyTable
   where ColumnX == :string
order by progressive_no desc
   limit 1;
```

Where :string is the string value you are looking for ('2' in your example) and :lastno is the progressive_no where you are looking from (5 in your example).

You will want a unique index on (ColumnX, progressive_no).