MySql使用现有表中的列创建新表,并从另一个表

时间:2017-05-11 19:17:49

标签: php mysql

我在数据库上有2个现有表,即Table_1Table_2

我需要将Table_1Table_2的信息合并到一个名为Combined_table的新表中。

Table_1列的名称和值可以直接复制到Combined_table

然后,我需要添加名称由Service DescriptionTable_2的不同值组成的列。每个不同的值必须成为添加到Combined_table的列名。

然后,这些新添加的列必须将其各自的值添加到正确的行中。可以通过所有表格中提供的MSISDN列来识别每一行。

Table_1

|MSISDN        |Abar Date  |Upgrd |Pack Code |Tariff               |Avg Spend |SIM Number
|--------------|-----------|------|----------|---------------------|----------|---------------------|
|9602005689344 |18-Mar-15  |Y     |MDA       |Machine2Machine Data |32.56     |89490000000000001215 |
|9602005926494 |06-Feb-15  |Y     |MLT       |Machine2Machine Lite |27.38     |89490000000000004457 |
|9602005926509 |06-Feb-15  |Y     |MLT       |Machine2Machine Lite |10.1      |89490000000000045677 |
|9602005926524 |06-Feb-15  |Y     |MLT       |Machine2Machine Lite |31.77     |89490000000000002887 |
|9602005926539 |18-Mar-15  |Y     |MDA       |Machine2Machine Data |32.36     |94900000000000100212 |
|9602006330948 |11-Mar-16  |N     |MLT       |Machine2Machine Lite |4.39      |89460000000015600111 |
|9602006330950 |11-Mar-16  |N     |MLT       |Machine2Machine Lite |4.39      |89940000000000426577 |
|--------------|-----------|------|----------|---------------------|----------|---------------------|

Table_2

|MSISDN         |Service Description               |Value     |
|---------------|----------------------------------|----------|
|9602005689344  |100MB                             |25.44     |
|9602005689344  |Basic Data 9600                   |0.00      |
|9602005689344  |Basic Telephony                   |6.58      |
|9602005689344  |Ebilling - Corporate CD Extract   |0.00      |
|9602005689344  |Unrestricted APN                  |0.00      |
|9602005926494  |100MB                             |25.44     |
|9602005926494  |Basic Data 9600                   |0.00      |
|9602005926494  |Basic Telephony                   |4.39      |
|9602005926494  |Ebilling - Corporate CD Extract   |0.00      |
|9602005926494  |Unrestricted APN                  |0.00      |
|9602005926509  |Basic Data 9600                   |0.00      |
|9602005926509  |Basic Telephony                   |4.39      |
|9602005926509  |Ebilling - Corporate CD Extract   |0.00      |
|9602005926509  |My Meg 0                          |0.00      |
|9602005926509  |Unrestricted APN                  |0.00      |
|9602005926524  |100MB                             |25.44     |
|9602005926524  |Basic Data 9600                   |0.00      |
|9602005926524  |Basic Telephony                   |4.39      |
|9602005926524  |Ebilling - Corporate CD Extract   |0.00      |
|9602005926524  |Unrestricted APN                  |0.00      |
|9602005926539  |100MB                             |25.44     |
|9602005926539  |Basic Data 9600                   |0.00      |
|9602005926539  |Basic Telephony                   |6.58      |
|9602005926539  |Ebilling - Corporate CD Extract   |0.00      |
|9602005926539  |Unrestricted APN                  |0.00      |
|9602006330948  |Basic Data 9600                   |0.00      |
|9602006330948  |Basic Telephony                   |4.39      |
|9602006330948  |Caller Identity FREE              |0.00      |
|9602006330948  |Ebilling - Corporate CD Extract   |0.00      |
|9602006330948  |My Meg 0                          |0.00      |
|9602006330948  |Promotional My Gig 1 - 24 Months  |0.00      |
|9602006330948  |SMS Mobile Originating            |0.00      |
|9602006330948  |Unrestricted APN                  |0.00      |
|9602006330950  |Basic Data 9600                   |0.00      |
|9602006330950  |Basic Telephony                   |4.39      |
|9602006330950  |Caller Identity FREE              |0.00      |
|9602006330950  |Ebilling - Corporate CD Extract   |0.00      |
|9602006330950  |My Meg 0                          |0.00      |
|9602006330950  |Promotional My Gig 1 - 24 Months  |0.00      |
|9602006330950  |SMS Mobile Originating            |0.00      |
|9602006330950  |Unrestricted APN                  |0.00      |
|---------------|----------------------------------|----------|

Combined_table的外观示例。

|MSISDN         |Abar Date  |Upgrd  |Pack   |Code Tariff            |Avg Spend  |SIM Number             |100MB  |Basic Data 9600    |Basic Telephony    |Caller Identity FREE   |Ebilling - Corporate CD Extract    |My Meg 0   |Promotional My Gig 1 - 24 Months   |SMS Mobile Originating |Unrestricted APN   |
|9602005689344  |18-Mar-15  |Y      |MDA    |Machine2Machine Data   |32.56      |89490000000000001215   |25.44  |0.00               |6.58               |0.00                   |0.00                               |0.00       |0.00                               |0.00                   |0.00               |
|9602005926494  |06-Feb-15  |Y      |MLT    |Machine2Machine Lite   |27.38      |89490000000000004457   |25.44  |0.00               |4.39               |0.00                   |0.00                               |0.00       |0.00                               |0.00                   |0.00               |
|9602005926509  |06-Feb-15  |Y      |MLT    |Machine2Machine Lite   |10.10      |89490000000000045677   |0.00   |0.00               |4.39               |0.00                   |0.00                               |0.00       |0.00                               |0.00                   |0.00               |
|9602005926524  |06-Feb-15  |Y      |MLT    |Machine2Machine Lite   |31.77      |89490000000000002887   |25.44  |0.00               |4.39               |0.00                   |0.00                               |0.00       |0.00                               |0.00                   |0.00               |
|9602005926539  |18-Mar-15  |Y      |MDA    |Machine2Machine Data   |32.36      |89490000000000100212   |25.44  |0.00               |6.58               |0.00                   |0.00                               |0.00       |0.00                               |0.00                   |0.00               |
|9602006330948  |11-Mar-16  |N      |MLT    |Machine2Machine Lite   |4.39       |89460000000015600111   |0.00   |0.00               |4.39               |0.00                   |0.00                               |0.00       |0.00                               |0.00                   |0.00               |
|9602006330950  |11-Mar-16  |N      |MLT    |Machine2Machine Lite   |4.39       |89940000000000426577   |0.00   |0.00               |4.39               |0.00                   |0.00                               |0.00       |0.00                               |0.00                   |0.00               |

我不知道如何使用PHP创建表。 我考虑过执行一个MySQL请求来创建和添加数据,但不知道如何开始。

任何建议都会非常有用。

2 个答案:

答案 0 :(得分:1)

如果您已经知道列名(即不同的描述),那么您可以创建一个表并编写SELECT查询,如下所示:

SELECT MSISDN, Abar Date, Upgrd, Pack Code, Tariff, Avg Spend, SIM Number,
(SELECT COALESCE(SUM(VALUE), 0) FROM Table_2 WHERE MSISDN = t1.MSISDN AND description = '100MB') AS 100MB,
(SELECT COALESCE(SUM(VALUE), 0) FROM Table_2 WHERE MSISDN = t1.MSISDN AND description = 'Basic Data 9600') AS 'Basic Data 9600',
(SELECT COALESCE(SUM(VALUE), 0) FROM Table_2 WHERE MSISDN = t1.MSISDN AND description = 'Basic Telephony') AS 'Basic Telephony',
(SELECT COALESCE(SUM(VALUE), 0) FROM Table_2 WHERE MSISDN = t1.MSISDN AND description = 'Ebilling - Corporate CD Extract') AS 'Ebilling - Corporate CD Extract',
(SELECT COALESCE(SUM(VALUE), 0) FROM Table_2 WHERE MSISDN = t1.MSISDN AND description = 'Unrestricted APN') AS 'Unrestricted APN'
FROM Table_1 t1;

您可以使用这些列创建一个表,并使用INSERT INTO .. SELECT语法,例如

INSERT INTO table_3
SELECT MSISDN, Abar Date, Upgrd, Pack Code, Tariff, Avg Spend, SIM Number,
(SELECT COALESCE(SUM(VALUE), 0) FROM Table_2 WHERE MSISDN = t1.MSISDN AND description = '100MB') AS 100MB,
(SELECT COALESCE(SUM(VALUE), 0) FROM Table_2 WHERE MSISDN = t1.MSISDN AND description = 'Basic Data 9600') AS 'Basic Data 9600',
(SELECT COALESCE(SUM(VALUE), 0) FROM Table_2 WHERE MSISDN = t1.MSISDN AND description = 'Basic Telephony') AS 'Basic Telephony',
(SELECT COALESCE(SUM(VALUE), 0) FROM Table_2 WHERE MSISDN = t1.MSISDN AND description = 'Ebilling - Corporate CD Extract') AS 'Ebilling - Corporate CD Extract',
(SELECT COALESCE(SUM(VALUE), 0) FROM Table_2 WHERE MSISDN = t1.MSISDN AND description = 'Unrestricted APN') AS 'Unrestricted APN'
FROM Table_1 t1;

答案 1 :(得分:0)

谢谢Darshan

我设法使用你的sugestions和来自实时数据库表的一些真实数据来编译以下内容。

创建工作正常的临时表:

CREATE table `infolog_SIMS` (`MSISDN` VARCHAR(50), `Abar Date` VARCHAR(50), `Term Date` VARCHAR(50), `Upgrd` VARCHAR(50), `Upgrd date` VARCHAR(50), `Pack Code` VARCHAR(50), `Tariff` VARCHAR(50), `Pack Desc` VARCHAR(50), `Avg Spend` VARCHAR(50), `Stat` VARCHAR(50), `SIM Number` VARCHAR(50));

然后在临时表中添加额外的列,也可以正常工作::

ALTER TABLE `infolog_SIMS` ADD COLUMN `100MB` VARCHAR(50) NOT NULL, ADD COLUMN `Basic Data 9600` VARCHAR(50) NOT NULL, ADD COLUMN `Basic Telephony` VARCHAR(50) NOT NULL, ADD COLUMN `Ebilling Corporate CD Extract` VARCHAR(50) NOT NULL, ADD COLUMN `LTE` VARCHAR(50) NOT NULL, ADD COLUMN `MMS Standard` VARCHAR(50) NOT NULL, ADD COLUMN `Unrestricted APN` VARCHAR(50) NOT NULL, ADD COLUMN `My Meg 0` VARCHAR(50) NOT NULL, ADD COLUMN `SMS Mobile Originating` VARCHAR(50) NOT NULL, ADD COLUMN `Caller Identity FREE` VARCHAR(50) NOT NULL, ADD COLUMN `Promotional My Gig 1 24 Months` VARCHAR(50) NOT NULL, ADD COLUMN `MyMeg 30 Mobile Internet Bundle` VARCHAR(50) NOT NULL, ADD COLUMN `15MB` VARCHAR(50) NOT NULL, ADD COLUMN `SPNwww.infolog.co.za` VARCHAR(50) NOT NULL, ADD COLUMN `Make Nat. Int. Roam Calls` VARCHAR(50) NOT NULL, ADD COLUMN `Outgoing Bar` VARCHAR(50) NOT NULL;

现在为每行添加值:

SELECT *, (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = '100MB') AS '100MB', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'Basic Data 9600') AS 'Basic Data 9600', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'Basic Telephony') AS 'Basic Telephony', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'Ebilling Corporate CD Extract') AS 'Ebilling Corporate CD Extract', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'LTE') AS 'LTE', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'MMS Standard') AS 'MMS Standard', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'Unrestricted APN') AS 'Unrestricted APN', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'My Meg 0') AS 'My Meg 0', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'SMS Mobile Originating') AS 'SMS Mobile Originating', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'Caller Identity FREE') AS 'Caller Identity FREE', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'Promotional My Gig 1 24 Months') AS 'Promotional My Gig 1 24 Months', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'MyMeg 30 Mobile Internet Bundle') AS 'MyMeg 30 Mobile Internet Bundle', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = '15MB') AS '15MB', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'SPNwww.infolog.co.za') AS 'SPNwww.infolog.co.za', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'Make Nat. Int. Roam Calls') AS 'Make Nat. Int. Roam Calls', (SELECT `Value` FROM `Infolog_temp_VAS` WHERE `Dialling No` = `t1`.`MSISDN` AND `Service Description` = 'Outgoing Bar') AS 'Outgoing Bar' FROM `Infolog_temp_SDR` `t1`;

如果Value表中不存在该服务,则某些NULL字段将返回Infolog_temp_VAS。是否可以在IF不存在的情况下添加service条件,以便将NULL更改为NO

请注意,这不是实际的插入命令,只是选择部分。