简体   繁体   English

将列名称与MYSQL中的数据匹配

[英]Match column name to data in MYSQL

I have data like in table. 我有表中的数据。

Item | 7/7/15 | 7/8/15 | 7/9/15
  1  |   23   |   24   |   25
  2  |   26   |   74   |   96

and

I have table which has, 我有桌子,

Item |  Date   | Number
1    | 7/9/15  | 56 
1    | 7/7/15  | 75 
1    | 7/8/15  | 63

I want to find sum of Number from 7/7/15 to 7/8/15 from table 1 and sum of the number from second table. 我想从表1中找到从7/7/157/8/15的数字总和,并从第二个表中找到数字的总和。

My table should look like 我的桌子应该看起来像

Item | StartDate | EndDate | no. | TotalNumber

item 7/7/15 7/8/15  7/9/15      
1   23      24     25
2   26      74     96

item date number
1   7/9/15  56
1   7/7/15  75
1   7/8/15  63

.

SELECT
    i1.Item,
    '7/7/15' AS "StartDate",
    '7/8/15' AS "EndDate",
    (SELECT SUM(`7/7/15`)+SUM(`7/8/15`) FROM table1 WHERE item=i1.item) AS no,
    (SELECT SUM(number) FROM table2 WHERE item=i1.item) "TotalNumber"
FROM
    table2 i2
RIGHT OUTER JOIN table1 i1 on i1.item=i2.item;
item startdate enddate no TotalNumber
1   7/7/15     7/8/15  47     194
1   7/7/15     7/8/15  47     194
1   7/7/15     7/8/15  47     194
2   7/7/15     7/8/15  100  

.

It's working.. 在工作

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

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