Now ALL IN ONE QUERY! Also, pretty comical IMHO.

Date: 2009-10-01 10:49 pm (UTC)
From: (Anonymous)
This should work in one query of pure SQL (no stored procedures)
but the efficiency is somewhat questionable. Also, it's like
a code comedy.

=========
sqlite> -- The key *directly* sought (without the next or prev rows)
sqlite> select key from pageviews where url like '%elf%' and key
...> > 10000
...> order by key asc limit 1;
10198
sqlite> -- Finds the key *directly* sought, also prev and next existing keys.
sqlite> select key from pageviews
...> where key in
...> (select key from pageviews where url like '%elf%' and key >10000

...> ORDER BY key ASC LIMIT 1)
...> -- find prev key
...> or key =
...> (select MAX(key) from pageviews where
...> key <
...> (select key from pageviews where url like '%elf%' and key >10000

...> ORDER BY KEY ASC LIMIT 1)
...> ORDER BY key DESC LIMIT 1)
...> -- find next key
...> or key =
...> (select MIN(key) from pageviews where
...> key >
...> (select key from pageviews where url like '%elf%' and key >10000

...> ORDER BY KEY ASC LIMIT 1)
...> ORDER BY key DESC LIMIT 1)
...> ;
10198
10197
10199
sqlite>
=========

--Cat Typist
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

elfs: (Default)
Elf Sternberg

June 2025

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 6th, 2025 12:06 am
Powered by Dreamwidth Studios