我想在表格中插入一系列值
create TEMPORARY TABLE IF NOT EXISTS currency_numbers(
num INT
);
insert into currency_numbers(num) VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
insert into pm_transaction_type (tran_type, fixed, rate, min_by_rate, max_by_rate, round, min_amount, max_amount, country_code, valid_since, valid_till, currency)
select 'fee.transfer_other_free', null, 0.1, 0, null, 0, null, null, "", null, null, currency_numbers.num
from currency_numbers
union
select 'fee.transfer_tip_free', null, 0.3, 4, null, 5, null, null, "", null, null, currency_numbers.num
from currency_numbers;
在这个例子中,我在2个select语句中使用union,但我想使用大约10的union。不幸的是,我收到以下错误:
Can't reopen table: 'currency_numbers'
如何编写此查询,希望每次都不必重复属性名称列表的编号?
答案 0 :(得分:1)
这是一个临时表问题 - 您不能在同一查询中多次引用TEMPORARY表。
作为一种解决方法,试试这个 -
SELECT t.*, c.* FROM currency_numbers c, (
SELECT 'fee.transfer_other_free' tran_type, null fixed, 0.1 rate, 0 min_by_rate, null max_by_rate, 0 round, null min_amount, null max_amount, '' country_code, null valid_since, null valid_till
UNION
SELECT 'fee.transfer_tip_free', null, 0.3, 4, null, 5, null, null, '', null, null
) t;
...及其INSERT版本 -
INSERT INTO pm_transaction_type (tran_type, fixed, rate, min_by_rate, max_by_rate, round, min_amount, max_amount, country_code, valid_since, valid_till, currency)
SELECT t.*, c.* FROM currency_numbers c, (
SELECT 'fee.transfer_other_free' tran_type, null fixed, 0.1 rate, 0 min_by_rate, null max_by_rate, 0 round, null min_amount, null max_amount, '' country_code, null valid_since, null valid_till
UNION
SELECT 'fee.transfer_tip_free', null, 0.3, 4, null, 5, null, null, '', null, null
) t;
答案 1 :(得分:0)
我没有对此进行测试,但您明白了这一点:
insert into pm_transaction_type (tran_type, fixed, rate, min_by_rate, max_by_rate, round, min_amount, max_amount, country_code, valid_since, valid_till, currency)
select 'fee.transfer_other_free', null, 0.1, 0, null, 0, null, null, "", null, null, currency_numbers.num
from currency_numbers cn1
union
select 'fee.transfer_tip_free', null, 0.3, 4, null, 5, null, null, "", null, null, currency_numbers.num
from currency_numbers cn2;