加入几个表

时间:2013-05-09 17:15:46

标签: sql database join

我的问题是关于SQL查询

使用下表:

1. consultant(id,Name,Skill) 
2. CustomerCompany(Id,name Address, Phone, Email, WebAddr,Market)
3. project(id,StartDate,EndDate,ConsultantID,CustomerId,Days)
4. Invoice(id,Date,Customer,Amount,Status) 
什么是

的SQL语句

找到在柏林和伦敦为客户服务的顾问的姓名

我认为你必须加入这些表格,但我无法确定查询,有什么建议吗?

3 个答案:

答案 0 :(得分:1)

这样的东西
SELECT C.Name
FROM Consultant C
--Join on projects the consultant worked on
JOIN Project P ON P.ConsultantID = C.ConsultantID
--Join on the customer companies those projects were for
JOIN CustomerCompany CC ON CC.ID = P.CustomerID
--Addresses might have various formats, use the LIKE operator
WHERE CC.Address LIKE '%London%' OR CC.Address LIKE '%Berlin%'

可能会成功。

答案 1 :(得分:0)

select 
con.Name
from CustomerCompany CustComp
inner join project PRO on PRO.CustomerId=CustComp.Id
inner join consultant con on con.id=PRO.ConsultantID
where CustComp.Address LIKE '%London%' OR CustComp.Address LIKE '%Berlin%'

<强> JOIN

答案 2 :(得分:0)

很少可以从您的问题中确定,但您可以这样做:

 SELECT Name from consultant c
 join CustomerCompany cc on c.id = cc.id
 WHERE market in ('London', 'Berlin')

基本联接就像这样

 Select columnname from FirstTable alias
 join secondtable alias on fieldFromFirstTable = FieldFromSecondTable
 WHERE criteria.