MySQL将单行值拆分为多个插入

时间:2015-01-01 22:54:33

标签: mysql database select insert

这是我目前的订单销售表结构(简化):

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 for MySQL,这是我到目前为止所做的:

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

但我还需要获取fee1 ... fee(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

有什么想法吗?

3 个答案:

答案 0 :(得分: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 |
+----+----------+--------+-------+---------------------+

答案 1 :(得分:0)

我知道更改数据库的结构需要进行许多其他更改,在许多情况下可能需要做很多工作,但我认为更改数据库的结构会使您的工作更加可扩展和直接。

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

FeeType(Fee_ID,Fee_Title)//所有费用类型(费用1,费用2,等等)都应存储在此处作为记录

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

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

答案 2 :(得分: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'列作为新'交易'的最后一列。 table,用它来进行连接,在完成将数据迁移到Transactions_fee表之后,删除临时的' Transactions.id'列。