microsoft sql从多个表中选择查询

时间:2015-05-27 03:48:34

标签: sql-server sql-server-2008

此查询在mysql中工作,但在microsoft sql server management studio 2008中无效,有人可以帮助我吗?

SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid 
FROM cms_question_report QR, 
cms_clients C,
cms_questioncategory QC ,
cms_reporthistory RH 
WHERE C.id=QR.cid 
AND QR.rhid=RH.id 
AND QR.catid='3' 
AND QR.catid=QC.id

我收到错误:Invalid object name cms_question_report

2 个答案:

答案 0 :(得分:1)

指定通常情况下,当您具有特定架构并且未指定它时,它会发生:

替换dbo。使用您的架构和/或键入您的数据库名称

SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid 
FROM databasename.dbo.cms_question_report QR, 
databasename.dbo.cms_clients C,
databasename.dbo.cms_questioncategory QC ,
databasename.dbo.cms_reporthistory RH 
WHERE C.id=QR.cid 
AND QR.rhid=RH.id 
AND QR.catid='3' 
AND QR.catid=QC.id

答案 1 :(得分:1)

SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid 
FROM cms_question_report QR
left join cms_clients C
on C.id=QR.cid 
left join cms_questioncategory QC
on QR.catid=QC.id
and QR.catid='3'
left join cms_reporthistory RH
on QR.rhid=RH.id

我认为应该这样做