我需要将这两个查询的结果合并为一个语句

时间:2015-05-06 18:30:55

标签: sql many-to-many

设置是联系人表,员工表和具有多对多关系的Employee_contact表。我想知道如何将这两个查询组合成一个用于组合结果集。

SELECT FirstName, LastName, (ContactNumber) AS Home  
FROM Employees AS E
JOIN Employees_Contacts AS EC ON E.EmployeeID = EC.EmployeeID
JOIN Contacts AS C on EC.ContactID = C.ContactID
WHERE ContactType = 'Home Phone'

SELECT FirstName, LastName, (ContactNumber) AS Fax
FROM Employees AS E
JOIN Employees_Contacts AS EC ON E.EmployeeID = EC.EmployeeID
JOIN Contacts AS C on EC.ContactID = C.ContactID
WHERE ContactType = 'Home Fax';

3 个答案:

答案 0 :(得分:1)

一种选择是使用条件聚合:

SELECT FirstName, LastName,
   MAX(CASE WHEN ContactType = 'Home Phone' THEN ContactNumber END) AS Home,
   MAX(CASE WHEN ContactType = 'Home Fax' THEN ContactNumber END) AS Fax
FROM Employees AS E
    JOIN Employees_Contacts AS EC ON E.EmployeeID = EC.EmployeeID
    JOIN Contacts AS C on EC.ContactID = C.ContactID
WHERE ContactType IN ('Home Phone','Home Fax')
GROUP BY FirstName, LastName

答案 1 :(得分:0)

使用案例陈述

UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint: CGPointMake(47.18, 5.32)];
[bezierPath addCurveToPoint: CGPointMake(67.96, 17.62) controlPoint1: CGPointMake(55.58, 7.5) controlPoint2: CGPointMake(61.06, 10.72)];
[bezierPath addCurveToPoint: CGPointMake(81.46, 49.5) controlPoint1: CGPointMake(77.56, 27.15) controlPoint2: CGPointMake(81.46, 36.45)];
[bezierPath addCurveToPoint: CGPointMake(67.96, 81.37) controlPoint1: CGPointMake(81.46, 62.55) controlPoint2: CGPointMake(77.56, 71.85)];
[bezierPath addCurveToPoint: CGPointMake(47.18, 93.67) controlPoint1: CGPointMake(61.06, 88.27) controlPoint2: CGPointMake(55.58, 91.5)];
[bezierPath addCurveToPoint: CGPointMake(24.98, 93.67) controlPoint1: CGPointMake(41.33, 95.17) controlPoint2: CGPointMake(30.83, 95.17)];
[bezierPath addCurveToPoint: CGPointMake(9.16, 86.32) controlPoint1: CGPointMake(19.81, 92.32) controlPoint2: CGPointMake(12.38, 88.87)];
[bezierPath addLineToPoint: CGPointMake(7.06, 84.52)];
[bezierPath addLineToPoint: CGPointMake(10.13, 80.47)];
[bezierPath addCurveToPoint: CGPointMake(19.13, 61.27) controlPoint1: CGPointMake(13.81, 75.52) controlPoint2: CGPointMake(18.01, 66.67)];
[bezierPath addCurveToPoint: CGPointMake(18.83, 38.25) controlPoint1: CGPointMake(20.33, 55.5) controlPoint2: CGPointMake(20.18, 43.42)];
[bezierPath addCurveToPoint: CGPointMake(9.98, 19.95) controlPoint1: CGPointMake(17.11, 31.57) controlPoint2: CGPointMake(14.03, 25.35)];
[bezierPath addLineToPoint: CGPointMake(6.31, 15.07)];
[bezierPath addLineToPoint: CGPointMake(8.78, 13.05)];
[bezierPath addCurveToPoint: CGPointMake(24.08, 5.55) controlPoint1: CGPointMake(12.16, 10.27) controlPoint2: CGPointMake(18.53, 7.12)];
[bezierPath addCurveToPoint: CGPointMake(47.18, 5.32) controlPoint1: CGPointMake(29.93, 3.82) controlPoint2: CGPointMake(41.11, 3.75)];
[bezierPath closePath];

self.shapeLayer = [CAShapeLayer new];
self.shapeLayer.path = bezierPath.CGPath;
self.shapeLayer.frame = CGRectMake(80, 80, 85, 98);
self.shapeLayer.strokeColor = [UIColor blueColor].CGColor;
self.shapeLayer.lineWidth = 4;
self.shapeLayer.fillColor = [UIColor lightGrayColor].CGColor;
[self.layer addSublayer:self.shapeLayer];

答案 2 :(得分:0)

取决于您期望的结果。如果有的话,这个将返回所有员工的第一个HomePhone和HomeFax:

SELECT FirstName, LastName,
   (select top 1 ContactNumber from Employees_Contacts AS EC 
                JOIN Contacts AS C on EC.ContactID = C.ContactID 
                where E.EmployeeID = EC.EmployeeID 
                and EC.ContactType = 'Home Phone') as HomePhone,
   (select top 1 ContactNumber from Employees_Contacts AS EC
                JOIN Contacts AS C on EC.ContactID = C.ContactID 
                where E.EmployeeID = EC.EmployeeID 
                and EC.ContactType = 'Home Fax') as Fax
FROM Employees AS E

您的查询返回所有电话和传真。