使用旧 table 的数据生成新的 table,乘以查找值

Generate new table with data from old table, multiplied by lookup value

在MySQL中,有这两个查询:

CREATE TABLE `country` (
  `name` varchar(100) NOT NULL,
  `value001` decimal(15,2) NOT NULL,
  `value002` decimal(15,2) NOT NULL,
  `value003` decimal(15,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO country (name, value001, value002, value003) VALUES('Bulgaria', 100, 200, 300),('Portugal', 5, 6, 7),('Belarus', 20, 30, 40);

CREATE TABLE `exchange-rate`(
  `name` varchar(100) NOT NULL,
  `rate` decimal(15,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `exchange-rate` (name, rate) VALUES('Bulgaria', 1.9),('Portugal', 1.1),('Belarus', 10);

我得到以下 tables:

我的想法是创建第三个 table,其中第一个 table 的值将乘以第二个的值。例如,对于白俄罗斯和葡萄牙,它将如下所示:

name value1 value2 value3
Belarus 200 300 400
Portugal 5.5 6.6 7.7

到目前为止我做了什么?

drop table if exists new_country;
create table if not exists new_country;
select c.*
from country c
left outer join --or something wiser?
(
--probably something is to be written here
)

您可以使用 create table as select 语法,并加入国家名称:

DROP TABLE IF exists new_country;
CREATE TABLE new_country AS
SELECT c.name AS name, 
       value001 * er.rate AS value1,
       value002 * er.rate AS value2,
       value003 * er.rate AS value3
FROM   test.country c
JOIN   `exchange-rate` er ON c.name = er.name;