2013-10-04 76 views
-1
select request_id, request_created_date, request_complaint_type, request_descriptor, 
request_ferry_terminal 
from nyc311_requests 
    join nyc311_locations_ferries on request_id=request_id 
where request_ferry_direction = 'Manhattan Bound' 
order by request_id 

我有這段代碼,當我嘗試執行時,它說每次提到request_id時,它都是一個不明確的列。如何指定何時連接到具有相同主鍵名稱的表?

+0

所有你需要的是[SQL別名](http://beginner-sql-tutorial.com/sql-aliases.htm) –

回答

2

你需要看到該表是從的,請注意我已經添加了表名到REQUEST_ID

select nyc311_requests.request_id, request_created_date, request_complaint_type, request_descriptor, 
request_ferry_terminal 
from nyc311_requests 
    join nyc311_locations_ferries on nyc311_requests.request_id=nyc311_locations_ferries.request_id 
where request_ferry_direction = 'Manhattan Bound' 
order by request_id 

,你也可以給表的別名

select r.request_id, request_created_date, request_complaint_type, request_descriptor, 
request_ferry_terminal 
from nyc311_requests r 
    join nyc311_locations_ferries lf on r.request_id=lf.request_id 
where request_ferry_direction = 'Manhattan Bound' 
order by request_id 
0

你必須給你的表別名。

select A.request_id, request_created_date, request_complaint_type, request_descriptor, 
request_ferry_terminal 
from nyc311_requests A 
    join nyc311_locations_ferries B on A.request_id = B.request_id 
where request_ferry_direction = 'Manhattan Bound' 
order by request_id 

你必須指定出現兩個表中的字段中的別名,但一旦你知道如何做到這一點是非常簡單的。

相關問題