On Sun, 24 May 2020 11:34:45 +0200, Luuk <luuk@invalid.lan> wrote:
Ok, example, selecting the two lowest and the two highest values:
WITH RECURSIVE cte (n) AS
(
SELECT 1
UNION ALL
SELECT n + 1 FROM cte WHERE n < 5
)
(SELECT *
FROM cte
order by n
limit 2 )
union all
(SELECT *
FROM cte
order by n desc
limit 2 )
order by n
;
1) The 'with recursive' creates a temporary view containing 5 records
from 1 until (including) 5.
2) In the 'SELECT * FROM cte order by n limit 2', the ORDER BY is needed
to make sure where get the first two records. Because of the LIMIT there
is also a need to know where to start, this can only be done using ORDER BY. >>
3) after the union all we do the same select, again with 'order by' to
make sure the highest two values are selected.
4) the final order by is needed to produce 1,2,4,5 and not 1,2,5,4
Sorry, that example is beyond me and I do not know what it is intended
to show. And I've never run across RECURSIVE before.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 113 |
Nodes: | 8 (1 / 7) |
Uptime: | 165:04:43 |
Calls: | 2,505 |
Calls today: | 2 |
Files: | 8,705 |
Messages: | 1,931,710 |