不明确的列'Site_name'

时间:2014-09-29 09:05:37

标签: sql asp.net

我有内连接两个表并显示。我尝试了下面给出的代码。但它会产生错误" Ambiguous Column site_name' "

SELECT site_name, Mains_Run_Hrs, DG_Run_Auto_Mode, Battery_Run_Hrs,Solar_Run_hrs 
FROM tbl_runtime_report INNER JOIN tbl_site_details 
ON tbl_runtime_report.Site_ID=tbl_site_details.site_id 
WHEREsite_name=@site_name

2 个答案:

答案 0 :(得分:1)

如果两个表中都有site_name,则需要指定要查询的版本,即

WHERE tbl_runtime_report.site_name = @site_name

或者,您可以使用别名

SELECT rt.site_name, ... 
FROM tbl_runtime_report rt ...
WHERE rt.site_name = @site_name

答案 1 :(得分:1)

SELECT  Report.ColumnName,Details.ColumnName
FROM tbl_runtime_report Report
     INNER JOIN tbl_site_details  Details ON Report.Site_ID=Details .site_id 
WHERE Report.site_name=@site_name -- use the relevant alias here 

请参阅When to use SQL Table Alias