I got the result of a query, and want to join it with a mockup table to fill the rows up to a certain number. this is an example where the query has two rows as result, but i always want that to fill it up to 5 (see column slot)
SELECT * FROM foo WHERE …
foo | bar |
---|---|
jhkl | jol |
das | das |
result:
slot | foo | bar |
---|---|---|
1 | jhkl | jol |
2 | das | das |
3 | NULL | NULL |
4 | NULL | NULL |
5 | NULL | NULL |
You help is highly appreciated!
2
Answers
You can use a recursive CTE to produce exactly 5 rows every time. For example:
Result:
See running example at db<>fiddle.
This should work on MySQL 5.7:
Output: