Quantcast
Channel: MySQL Forums - Install & Repo
Viewing all articles
Browse latest Browse all 7244

selecting more than one subset of the result from a mysql result(using limit) (no replies)

$
0
0
Hi,
Please consider the following table as an example to explain my problem..
id name
1 a
2 b
4 c
7 d
9 e
11 f
12 g
13 h
15 i
16 j
18 k
21 l
23 m
25 n
26 0
Suppose i want to get 3(count) successive names starting from id = 4(row 3) from table x, i do following methods to obtain it ...
method 1 - select name from x limit 3,3 (when i know row number where id = 4)
method 2 - select name from x where id >= 4 limit 3 (when i dont know row number)
and i obtain result as
id name
4 c
7 d
9 e

Now my problem is that i want a result of the 3(count) successive names where id >= 2 & id >=13 & id>=23 .. that is result set should be

2 b
4 c
7 d

13 h
15 i
16 j

23 m
25 n
26 0

in continous order.In other words i want to tell to ignore/leave row with ids 1,9,11,12,18,21 .But i dont have ids which have to be ignored instead have the starting id(2,13,23) and the successive count(3 in this case) that has to be retrieved.

One solution to this is using union i.e with following query..
(select name from x where id>=2 limit 3) union (select name from x where id>=13 limit 3) union (select name from x where id>=23)

Is there any method to obtain it by scanning through table once and without using limit(as using union will scan the table as many times as select is present- in my example it will scan thrice(as i don want to use primary key and indexing on anything))?The reason i want solution without union is that i cannot afford to scan tables more times as there are more number of starting stables and table size is big.

Hope i made my problem clear to all..

Thanks in Advance,
Sukruthi

Viewing all articles
Browse latest Browse all 7244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>