SO,
I have an issue about query in MySQL DBMS. I have table repeats
with data, like:
+--------+-----+ | record | num | +--------+-----+ | foo | 2 | +--------+-----+ | bar | 3 | +--------+-----+ | baz | 1 | +--------+-----+
here record
field contains some data (irrelevant, let it be simple string) and num
field contains number of repeats of current row in the result set. So, I want to result in:
foo foo bar bar bar baz
i.e. 'foo'
was repeated twice, 'bar'
- three times and 'baz'
- once.
Ok, now what I've tried. I've 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
till MAX(num)
from repeats
table. After this, I can achieve my goal via:
SELECT
`repeats`.record
FROM
`natural`
LEFT JOIN `repeats`
ON `natural`.id<=`repeats`.num
WHERE
`repeats`.record IS NOT NULL
-this works well, but I don't want to use natural
table. And so, my question is: is it possible to achieve my goal with repetition of rows in a single SQL-query, without creating temporary data-structure? (Since it is MySQL, tricks with variables will also fit - I just have no idea now of how to do that). However, answer 'no, this is impossible because ..' will also be welcome.
I also suspect that it is some 'common' problem, but have not found any corresponding info, unfortunately. Update: here is a similar problem (yet in the same time - not fully similar since it also upcomes to sequence generation problem which I want to avoid)