SO
I have a problem with a query in the MySQL DBMS. I have a repeats
table with data, for example:
+ -------- + ----- +
| record | num |
+ -------- + ----- +
| foo | 2 |
+ -------- + ----- +
| bar | 3 |
+ -------- + ----- +
| baz | 1 |
+ -------- + ----- +
here, the record
field contains some data (it does not matter, let it be a simple line), and the num
field contains the number of repetitions of the current line in the result set. So, I want to bring to:
foo
foo
bar
bar
bar
baz
i.e. 'foo'
repeated twice, 'bar'
is three times and 'baz'
is once.
Ok, now I tried. I created a table:
CREATE TABLE `natural` ( `id` int(11) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB
- then filled it with natural numbers, from 1
to MAX(num)
from repeats
. After that, I can achieve my goal with:
SELECT `repeats`.record FROM `natural` LEFT JOIN `repeats` ON `natural`.id<=`repeats`.num WHERE `repeats`.record IS NOT NULL
- this works well, but I do not want to use the natural
table. So my question is: is it possible to achieve my goal of repeating rows in a single SQL query without creating a temporary data structure? (Since this is MySQL, tricks with variables are also suitable - I just don't know how to do this). However, answer "no, this is not possible because .." will also be welcome.
I also suspect that this is some kind of βgeneralβ problem, but, unfortunately, I did not find any relevant information. Update: there is a similar problem here (but at the same time itβs not exactly the same, as it also goes back to the problem of generating a sequence, which I want to avoid)