简体   繁体   English

使用表中的值更新 mysql 数据库

[英]Update mysql database using values from table

I have made some changes to an existing mysql database structure and want to update old values to new fields in a table using an id from another table.我对现有的 mysql 数据库结构进行了一些更改,并希望使用另一个表中的 id 将旧值更新为表中的新字段。 The structures are as follows:结构如下:

table1表格1

+---------+--------+
| sale_id | type   |
+---------+--------+
| 110     | credit |
| 111     | cash   |
| 112     | credit |
+---------+--------+

table2表2

+---------+--------+---------+-----------------+---------------+
| sale_id | item_id|   price |  payment_type   |  total_amount |
+---------+--------+---------+-----------------+---------------+
| 110     | 1      |   20    |                 |               |    
| 111     | 2      |   30    |                 |               |
| 112     | 3      |   45    |                 |               |
| 112     | 4      |   15    |                 |               |
+---------+--------+---------+-----------------+---------------+

Now I want to update the payment_type and total_amount in table2 using sale_id from table1 as reference.现在我想使用 table1 中的 sale_id 作为参考来更新 table2 中的 payment_type 和 total_amount。

Expected result after table2 is updated table2 更新后的预期结果

+---------+--------+---------+-----------------+---------------+
| sale_id | item_id|   price |  payment_type   |  total_amount |
+---------+--------+---------+-----------------+---------------+
| 110     | 1      |   20    |      credit     |      20       |    
| 111     | 2      |   30    |      cash       |      30       |
| 112     | 3      |   45    |      credit     |      45       |
| 112     | 4      |   15    |      credit     |      15       |
+---------+--------+---------+-----------------+---------------+

My current attempt is not working and throwing an error.我当前的尝试不起作用并引发错误。

Example on fidddle 小提琴的例子

CREATE TABLE table1(
   sale_id INTEGER  NOT NULL PRIMARY KEY 
  ,type  VARCHAR(6) NOT NULL
);
INSERT INTO table1(sale_id,type) VALUES (110,'Credit');
INSERT INTO table1(sale_id,type) VALUES (111,'Cash');
INSERT INTO table1(sale_id,type) VALUES (112,'Credit');


CREATE TABLE table2(
   sales_id INTEGER  NOT NULL PRIMARY KEY 
  ,item_id  VARCHAR(6) NOT NULL
  ,price  VARCHAR(6) NOT NULL
  ,payment_type  VARCHAR(6) NOT NULL
  ,total_amount  VARCHAR(6) NOT NULL
);

INSERT INTO table2(sales_id,item_id,price,payment_type,total_amount) VALUES (110,1,20,'','');
INSERT INTO table2(sales_id,item_id,price,payment_type,total_amount) VALUES (111,2,30,'','');
INSERT INTO table2(sales_id,item_id,price,payment_type,total_amount) VALUES (112,3,45,'','');
INSERT INTO table2(sales_id,item_id,price,payment_type,total_amount) VALUES (112,4,15,'','');


UPDATE table2 SET total_amount = 
    (SELECT price
        FROM table2
        INNER JOIN table1 ON sale_id = sales_id
        );
UPDATE table2 SET payment_type = 
    (SELECT type
        FROM table2
        INNER JOIN table1 ON sale_id = sales_id
        );

    
    

That's the update/join syntax:这就是更新/加入语法:

update table2 t2
inner join table1 t1 on t1.sale_id = t2.sale_id
set t2.payment_type = t1.type, t2.total_amount = t2.price

You can update table using update clause您可以使用更新子句更新表

UPDATE table2
INNER JOIN table1
ON table2.sale_id = table1.sale_id
SET table2.payment_type = table1.type
    table2.total_amount = table2.price ;

FOR MORE PRACTICE ON UPDATE JOIN PLEASE FOLLOW BELOW LINK有关更新加入的更多实践,请点击以下链接

UPDATE JOIN更新加入

I think you try to use wrong approach.我认为您尝试使用错误的方法。 Instead normalize data and use JOIN you going to de-normalize data.取而代之的是规范化数据并使用 JOIN 来反规范化数据。 I advice to use different tables for sales and sale_items, but fetch data from both tables on demand like:我建议对 sales 和 sale_items 使用不同的表,但可以按需从两个表中获取数据,例如:

CREATE TABLE payment_types (
   id INTEGER  NOT NULL PRIMARY KEY 
  ,type  VARCHAR(6) NOT NULL
);
INSERT INTO payment_types VALUES (1,'Credit'), (2,'Cash');

CREATE TABLE sales (
   id INTEGER  NOT NULL PRIMARY KEY 
  ,payment_type_id  INTEGER NOT NULL
);
INSERT INTO sales(id,payment_type_id) VALUES (110,1), (111,2), (112,1);

CREATE TABLE sale_items (
   sale_id INTEGER  NOT NULL
  ,item_id  INTEGER NOT NULL
  ,price  INTEGER NOT NULL
  ,quantity  INTEGER NOT NULL
);

INSERT INTO sale_items (sale_id,item_id,price,quantity) VALUES 
(110,1,20,1), (111,2,30,2), (112,3,45,1), (112,4,15,1);

select 
    sale_id,
    pt.type,
    si.*,
    price * quantity as total_amount
from sales s
join payment_types pt on pt.id = s.payment_type_id
join sale_items si on si.sale_id = s.id;

Try SQL online here Result 在此处在线尝试 SQL结果

+=========+========+=========+=======+==========+==============+
| sale_id | type   | item_id | price | quantity | total_amount |
+=========+========+=========+=======+==========+==============+
| 110     | Credit | 1       | 20    | 1        | 20           |
+---------+--------+---------+-------+----------+--------------+
| 111     | Cash   | 2       | 30    | 2        | 60           |
+---------+--------+---------+-------+----------+--------------+
| 112     | Credit | 3       | 45    | 1        | 45           |
+---------+--------+---------+-------+----------+--------------+
| 112     | Credit | 4       | 15    | 1        | 15           |
+---------+--------+---------+-------+----------+--------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM