2012-01-03 61 views
0

我要插入值的范围为我的表插入范围

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语句的联合,价值观(MySQL的),但我想用联盟与约10不幸的是,我收到以下错误:

Can't reopen table: 'currency_numbers' 

我怎么可以这样写查询,希望无需重复或者每一次的属性名称的列表的数目?

回答

1

这是一个临时表的问题 - 你不能引用到一个临时表不止一次在相同的查询。

TEMPORARY Table Problems

作为一种变通方法,尝试这一个 -

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; 
+0

我只是认为我会添加一个不是第一个select子句需要标签的原因是否则MySQL会抱怨重复的空列 – Casebash 2012-01-03 23:29:12

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; 
+0

一个有趣的想法,但没有奏效 – Casebash 2012-01-03 23:16:27