我在SQL Server中有两个表
table1具有CustomerID,CustomerName,table2具有CustomerID,Contact。现在我必须找到在表2中有两个联系号码的客户名称和ID。
我已经编写了查询,但它只显示了客户的ID而不是名称:
SELECT distinct tbl_Cust1.CustID, tbl_Cust1.CustName
FROM tbl_Cust1 INNER JOIN
tbl_Cust2 ON tbl_Cust1.CustID =tbl_Cust2.CustID
and tbl_Cust2.CustID in (SELECT tbl_Cust2.CustID
FROM tbl_Cust2
GROUP BY CustID
HAVING (COUNT(*) = 2))
答案 0 :(得分:1)
试试这个:
SELECT DISTINCT t1.CustID, t1.CustName
FROM tbl_Cust1 t1
JOIN tbl_Cust2 t2 ON t1.CustID = t2.CustID
GROUP BY CustID, t1.CustName
HAVING (COUNT(t2.Contact) = 2))