2015-01-01 39 views
2

这是我目前的订单销售表结构(简体):的MySQL拆分单列值到多个插入

id|order_id|fee1|fee2|fee3|fee4|fee5|fee6|fee7|fee8|total|created 

这是我想它成为什么:

交易:

id|order_id|total|created 

Transaction_fee:

id|transaction_id|fee|amount 

我会如何去处理原始订单销售记录,并将它们转移到这个新的2表结构中?

我知道的INSERT ... SELECT的MySQL,这是我到目前为止有:

INSERT INTO transactions (order_id, total, created) 
     SELECT order_id, total, created 
     FROM order_sales 

但我也需要抓住费用1 ...费(n)和把那些进入transaction_fee表中,最好在与费用表相同的查询中将交易主键作为外键,因此需要准确。

所以就变成:

Transactions 
1|123|50.00|2015-01-01 00:00:00 

Transaction_fee 
1|1|fee1|10.00 
1|1|fee2|10.00 
1|1|fee3|10.00 
1|1|fee4|10.00 
1|1|fee5|10.00 

有什么想法?

+0

将此描述为结构是对该术语过于慷慨的使用。但是我知道你要去哪里,这很好。 – Strawberry

+0

当前表中的'order_id'是否唯一? – sn00k4h

+0

@ sn00k4h这是一个合理的假设 – Strawberry

回答

0

我知道改变数据库的结构需要许多其他的改变,并且在很多情况下可能会有很多工作,但是我认为改变数据库的结构将使得你的工作更加可扩展和直接。

例如,如果您希望在未来添加新类型的费用,则需要添加一个新列并编辑许多现有查询,因此我建议将您的销售表分成3个相关表格: Orders(OrderID,Total ,创建日期)//不确定是否需要总数可以通过查询选择我正在添加它以防万一

FeeType(Fee_ID,Fee_Title)//所有费用类型(fee1,fee2,.. etc )应该作为记录存储在这里

OrderFees(OrderID,Fees_ID,Amount)//这里我假设付费金额是每次添加的,如果不是,您可以在FeeTypes表中设置“金额”。

现在如果你想发送一个事务,它可能是一个完整的插入语句到订单表,如果你只是想保存订单的总结,否则如果你想发送有关每个费用的详细信息,你将发送插入查询到OrderFees表,包含OrderID,FeeID和应付金额。

+1

我认为这就是OP所要做的。 – Strawberry

1

考虑以下...

DROP TABLE IF EXISTS transactions; 

CREATE TABLE transactions 
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY 
,order_id INT NOT NULL 
,fee1 DECIMAL(5,2) NOT NULL 
,fee2 DECIMAL(5,2) NOT NULL 
,fee3 DECIMAL(5,2) NOT NULL 
,total DECIMAL(5,2) NOT NULL 
,created DATETIME NOT NULL 
); 

INSERT INTO transactions VALUES 
(1,123,50.00,75.00,25.00,150.00,'2015-01-01 00:00:00'), 
(2,125,10.00,25.00,15.00,50.00,'2015-01-02 00:00:00'); 


SELECT * FROM transactions; 

+----+----------+-------+-------+-------+--------+---------------------+ 
| id | order_id | fee1 | fee2 | fee3 | total | created    | 
+----+----------+-------+-------+-------+--------+---------------------+ 
| 1 |  123 | 50.00 | 75.00 | 25.00 | 150.00 | 2015-01-01 00:00:00 | 
| 2 |  125 | 10.00 | 25.00 | 15.00 | 50.00 | 2015-01-02 00:00:00 | 
+----+----------+-------+-------+-------+--------+---------------------+ 

SELECT id 
    , order_id 
    , 1 fee_id 
    , fee1 value 
    , created 
    FROM transactions 
UNION 
SELECT id 
    , order_id 
    , 2 
    , fee1 
    , created 
    FROM transactions 
UNION 
SELECT id 
    , order_id 
    , 3 
    , fee1 
    , created 
    FROM transactions; 
+----+----------+--------+-------+---------------------+ 
| id | order_id | fee_id | value | created    | 
+----+----------+--------+-------+---------------------+ 
| 1 |  123 |  1 | 50.00 | 2015-01-01 00:00:00 | 
| 2 |  125 |  1 | 10.00 | 2015-01-02 00:00:00 | 
| 1 |  123 |  2 | 50.00 | 2015-01-01 00:00:00 | 
| 2 |  125 |  2 | 10.00 | 2015-01-02 00:00:00 | 
| 1 |  123 |  3 | 50.00 | 2015-01-01 00:00:00 | 
| 2 |  125 |  3 | 10.00 | 2015-01-02 00:00:00 | 
+----+----------+--------+-------+---------------------+ 


DROP TABLE IF EXISTS transactions_new; 

CREATE TABLE transactions_new AS 
SELECT id 
    , order_id 
    , 1 fee_id 
    , fee1 value 
    , created 
    FROM transactions 
UNION 
SELECT id 
    , order_id 
    , 2 
    , fee1 
    , created 
    FROM transactions 
UNION 
SELECT id 
    , order_id 
    , 3 
    , fee1 
    , created 
    FROM transactions; 

ALTER TABLE transactions_new ADD PRIMARY KEY(id,order_id,fee_id); 

SELECT * FROM transactions_new; 
+----+----------+--------+-------+---------------------+ 
| id | order_id | fee_id | value | created    | 
+----+----------+--------+-------+---------------------+ 
| 1 |  123 |  1 | 50.00 | 2015-01-01 00:00:00 | 
| 1 |  123 |  2 | 50.00 | 2015-01-01 00:00:00 | 
| 1 |  123 |  3 | 50.00 | 2015-01-01 00:00:00 | 
| 2 |  125 |  1 | 10.00 | 2015-01-02 00:00:00 | 
| 2 |  125 |  2 | 10.00 | 2015-01-02 00:00:00 | 
| 2 |  125 |  3 | 10.00 | 2015-01-02 00:00:00 | 
+----+----------+--------+-------+---------------------+ 
+0

我知道'created'专栏应该留在'transactions'中。 – RandomSeed

0

如果 'ORDER_ID' 列是唯一:

INSERT INTO Transaction_fee(transaction_id, fee, amount) 
    SELECT t.id, 'fee1', o.fee1 
    FROM Transactions t 
    LEFT JOIN Order o USING(order_id) -- Assuming your current table is called 'Order' 
    UNION ALL 
    SELECT t.id, 'fee2', o.fee2 
    FROM Transactions t 
    LEFT JOIN Order o USING(order_id) 
    UNION ALL 
    ... 

如果ORDER_ID不是唯一的,你仍然可以实现相同的方法,但使用现有订单表的“ID”列。我要做的是暂时将这个'id'列作为新'Transactions'表的最后一列,用它来完成加入,并且在完成将数据迁移到您的Transactions_fee表后,删除临时'Transactions'。 ID'栏。