SQL Insert Select x times

Viewed 33

I have an INSERT SELECT query which works to insert a single record, but I need it to insert the same record x times, where x is a field returned from the database.

Here is the SQL:

INSERT INTO pool(stop_id) 
SELECT stop_id
FROM parts p 
LEFT JOIN stops s ON s.id = p.stop_id
WHERE p.place_id = 5054 AND s.limit IS NOT NULL;

Here is an attempt to insert x times which fails with syntax error:

INSERT INTO pool(stop_id) 
SELECT stop_id
FROM parts p 
LEFT JOIN stops s ON s.id = p.stop_id
WHERE p.place_id = 5054 AND s.limit IS NOT NULL
GO s.limit;

SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GO s.limit' at line 6
1 Answers

Mysql doesn't have a GO

There query would look like,

The order is vital, as tables are by nature unsorted

INSERT INTO pool(stop_id) 
SELECT stop_id
FROM parts p 
LEFT JOIN stops s ON s.id = p.stop_id
WHERE p.place_id = 5054 AND s.limit IS NOT NULL
ORDER BY s.limit
LIMIT 1000;
Related