在SQL中合并两个派生表

时间:2018-02-15 19:49:20

标签: mysql sql

通过以下SQL查询,我得到下表

SELECT COUNT(Specialist_Category.Specialist_ID), Specialist_Category_Name.Specialist_Category_Name 
FROM Specialist_Category, Specialist_Category_Name 
WHERE Specialist_Category.Category_ID = Specialist_Category_Name.Specialist_Category_ID 
GROUP BY Specialist_Category_Name.Specialist_Category_Name 

+------------------------------------------+--------------------------+
| COUNT(Specialist_Category.Specialist_ID) | Specialist_Category_Name |
+------------------------------------------+--------------------------+
| 3                                        | Mouse                    |
+------------------------------------------+--------------------------+
| 2                                        | Network                  |
+------------------------------------------+--------------------------+
| 2                                        | Keyboard                 |
+------------------------------------------+--------------------------+

我也使用下面的查询

获取下表
SELECT COUNT(Problem.Problem_ID), Problem_Category.Category_Name 
FROM Problem, Problem_Category 
WHERE Problem.Category_ID = Problem_Category.Category_ID 
GROUP BY Problem_Category.Category_Name 

+---------------------------+---------------+
| COUNT(Problem.Problem_ID) | Category_Name |
+---------------------------+---------------+
| 3                         | Keyboard      |
+---------------------------+---------------+
| 1                         | Mouse         |
+---------------------------+---------------+
| 1                         | Network       |
+---------------------------+---------------+
| 2                         | Printer       |
+---------------------------+---------------+

我正在尝试构建一个可以合并Speciality_Category_Name = Category_Name上的两个表的查询,这意味着Category_Name中但不会Specialist_Category_Name中出现的类别不会合并到新的+------------------------------------------+--------------------------+---------------------------+ | COUNT(Specialist_Category.Specialist_ID) | Specialist_Category_Name | COUNT(Problem.Problem_ID) | +------------------------------------------+--------------------------+---------------------------+ | 3 | Mouse | 1 | +------------------------------------------+--------------------------+---------------------------+ | 2 | Network | 1 | +------------------------------------------+--------------------------+---------------------------+ | 2 | Keyboard | 3 | +------------------------------------------+--------------------------+---------------------------+ 中表

即。预期的结果是

(SELECT COUNT(Specialist_Category.Specialist_ID), Specialist_Category_Name.Specialist_Category_Name, COUNT(Problem.Problem_ID)
FROM Specialist_Category, Specialist_Category_Name, 
    (SELECT COUNT(Problem.Problem_ID), Problem_Category.Category_Name 
    FROM Problem, Problem_Category 
    WHERE Problem.Category_ID = Problem_Category.Category_ID 
    GROUP BY Problem_Category.Category_Name) 
WHERE Specialist_Category.Category_ID = Specialist_Category_Name.Specialist_Category_ID 
GROUP BY Specialist_Category_Name.Specialist_Category_Name ) 

我尝试了一些使用外连接,内连接,联合的查询,但无济于事。

任何帮助都将不胜感激,谢谢。

我试过了

(SELECT COUNT(Specialist_Category.Specialist_ID), Specialist_Category_Name.Specialist_Category_Name
FROM Specialist_Category, Specialist_Category_Name
WHERE Specialist_Category.Category_ID = Specialist_Category_Name.Specialist_Category_ID 
GROUP BY Specialist_Category_Name.Specialist_Category_Name ) 

INNER JOIN 

(SELECT COUNT(Problem.Problem_ID), Problem_Category.Category_Name 
FROM Problem, Problem_Category 
WHERE Problem.Category_ID = Problem_Category.Category_ID 
GROUP BY Problem_Category.Category_Name) 

ON Problem_Category.Category_Name = Problem_Category.Category_Name 

AND

Specialist_Category_Name.Specialist_Category_Name

编辑:

我非常接近使用的结果。但是,我仍然不确定如何只选择Problem.Category_Name而不是SELECT * FROM ( SELECT COUNT(Specialist_Category.Specialist_ID), Specialist_Category_Name.Specialist_Category_Name FROM Specialist_Category, Specialist_Category_Name WHERE Specialist_Category.Category_ID = Specialist_Category_Name.Specialist_Category_ID GROUP BY Specialist_Category_Name.Specialist_Category_Name ) t1 INNER JOIN ( SELECT COUNT(Problem.Problem_ID), Problem_Category.Category_Name FROM Problem, Problem_Category WHERE Problem.Category_ID = Problem_Category.Category_ID GROUP BY Problem_Category.Category_Name ) t2 ON t1.Specialist_Category_Name = t2.Category_Name ,而不是在查询的最开始使用*

function split(s, delimiter)
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end

s = split("10:00:00.00",':')

for key, value in pairs(s) do
    print(key..'='..value)
end

0 个答案:

没有答案