Double insert in one INSERT..SELECT query
For example, I have the following code:
INSERT INTO table_prices(price, comment)
SELECT a.price, 'first'
FROM first_price AS a
UNION
SELECT a.price, 'second'
FROM second_price AS a;
And i have a result in table "table_prices":
id price comment
1 first_price1 first
2 first_price2 first
3 first_price3 first
... ... ...
15 second_price1 second
16 second_price2 second
... ... ...
But I need the following:
id price comment
1 first_price1 first
2 开发者_开发问答 second_price1 second
3 first_price2 first
4 second_price2 second
... ... ...
Could you help me - how can I achieve this? TIA!
you can use ORDER BY
ORDER BY id
Ordering rows in union selects requires temporary table, something like this should work:
INSERT INTO table_prices(price, comment)
SELECT `price`,`comment`
FROM (
SELECT a.price AS `price`, 'first' as `comment`
FROM first_price AS a
UNION
SELECT a.price, 'second'
FROM second_price AS a
) temp_table
ORDER BY `price`
精彩评论