2012-01-03 58 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