Mysql Query将3个表合并为1个表

时间:2016-07-08 06:54:00

标签: mysql

要绘制每个输入表我有单独的查询,需要对该查询应用功能并想要为输出表创建单个查询

从中选择不同的名称,SUM(计数) (选择查询表1 联盟 选择查询表2 联盟 按名称选择查询表3)表组;

此查询未正确添加计数Niether正确排序名称这有什么问题?

输入表1: -

    Names count  
    bob   3
    pol   4

输入表2: -

    Names count    
    bob   5  
          0      - name may be missing here neglect this entry  

输入表3: -

    Names count  
    james  4  
    pol    7  
    bob    1

预期输出表: -

    Names  count  
    bob    9  
    pol    11  
    james  4  

2 个答案:

答案 0 :(得分:0)

你可以使用UNION和它们的总和。

var pattern = "^[a-z0-9]+$"; // This is should accept on alpha numeric characters.
var reg = new RegExp(pattern, "i");
console.log("Should return false : "+reg.test("This $hould return false"));
console.log("Should return true : "+reg.test("Thisshouldreturntrue"));

答案 1 :(得分:0)

尝试此查询

select `Name`,sum(`Count`) total from ( select `Name`,`Count` from `table1` union all select `Name`,`Count` from `table2` union all select `Name`,`Count` from `table3` ) tot group by `Name`

愿这对你有所帮助。