select * from
(
(SELECT * FROM galery_1 WHERE id_galery < 12 ORDER BY id_galery DESC LIMIT 1) A
)
UNION
SELECT * FROM galery_1 WHERE id_galery >= 12 ORDER BY id_galery ASC limit 3
Oh, yeah...forgot. MySQL wants you to put parens around the SELECTs if you will use ORDER BY with them:
Code:
( SELECT * FROM table WHERE field < 13 ORDER BY field DESC LIMIT 1 )
UNION
( SELECT * FROM table WHERE field >= 13 ORDER BY field ASC LIMIT 2 )
Just tested it. That works.
Sure, you could also do a three way UNION, but then it has to be part of a sub-select:
Code:
SELECT * FROM table WHERE field IN (
SELECT max(field) FROM table WHERE field < 13
UNION
SELECT field FROM table WHERE field = 13
UNION
SELECT min(field) FROM table WHERE field > 13 )
ORDER BY field;
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
hmm I dont know why your first version was not working when I was trying, now it works (-:
anyway it would be interesting if it could be done in a way as at given link both prev and next in one query, so result would be in shape of row, like:
Select
( SELECT * FROM table WHERE field < 13 ORDER BY field DESC LIMIT 1 ) as prev
13,
( SELECT * FROM table WHERE field >13 ORDER BY field ASC LIMIT 1 ) as next
from table
this is complete for all records, gives you upper and lower neighbour
Code:
select P.Prev,N.id_galery as center, N.NEXT from
(SELECT a.id_galery, MIN(b.id_galery) AS Next
FROM galery_1 AS a
JOIN galery_1 AS b ON b.id_galery > a.id_galery
GROUP BY a.id_galery) as N
JOIN
(SELECT a.id_galery, MIN(b.id_galery) AS Prev
FROM galery_1 AS a
right JOIN galery_1 AS b ON a.id_galery > b.id_galery
GROUP BY b.id_galery) as P
on N.id_galery = P.id_galery
like:
Code:
8 13 24
13 24 56
24 56 58
note: this one hangs on not so big amount of data, even if id is indexed.
Last edited by BubikolRamios; 12-16-2009 at 09:11 AM..