2012-05-29 44 views
1

我正在努力獲得這個工作!我知道它的簡單...從SQL中的其他表中返回項目

我有滿滿一桌子的工作,新的位置可以動態地添加含有表(按順序並填充下拉列表等

我想這樣做的回報是什麼在一個位置(不是問題),所有的工作,他們分配了每個位置的ID(其中包含job_location_ID,job_Location_name,job_Location_Country)

sql = "SELECT [job_ID], 
     [job_ref], 
     [job_title], 
     [job_description], 
     [job_location] 
FROM [Web_Xtr_HR] 
WHERE (
      ([job_ext_int_both] = 1) 
      AND ([job_location] = " + theIndex + ") 
     ) 
ORDER BY [job_ID] DESC" 

這讀取所有的物品,所以我需要對應的地點ID要做的是,每個結果包括與其他表中的job_Location_ID關聯的job_Location_name([W eb_Xtr_Job_Locations])

help!

+2

這將是第一個約20評論通知您您的代碼很容易受到SQL注入攻擊。你應該考慮使用命令參數:http://forums.asp.net/t/886691.aspx/1 – SQLMason

回答

1

使用內部聯接是這樣的:

"SELECT [Web_Xtr_Job_Locations].job_Location_name, 
     [Web_Xtr_HR].[job_ID], 
     [Web_Xtr_HR].[job_ref], 
     [Web_Xtr_HR].[job_title], 
     [Web_Xtr_HR].[job_description], 
     [Web_Xtr_HR].[job_location] 
FROM [Web_Xtr_HR] 
     INNER JOIN [Web_Xtr_Job_Locations] 
      ON [Web_Xtr_HR].job_location_id = [Web_Xtr_Job_Locations].job_location_id 
WHERE (
      ([Web_Xtr_HR].[job_ext_int_both] = 1) 
      AND ([Web_Xtr_HR].[job_location] = " + theIndex + ") 
     ) 
ORDER BY [job_ID] DESC" 
1

你需要的是加入這兩個表,使它們可以從它們中選擇列。

SELECT job_ID, 
     job_title, 
     job_Location_Name 
FROM Web_Xtr_HR 
     INNER JOIN Web_Xtr_Job_Locations 
      ON Web_Xtr_Hr.job_Location_ID = Web_Xtr_Job_Locations.job_LocationID 
WHERE job_ext_int_both = 1 -- Add more filters as needed. 

INNER JOIN將回到那裏是在這兩個表中匹配的元素。匹配的字段由ON行確定。

+0

我一直在嘗試,但我想我陷入了混亂! 我會在你的語法之後有另一個刺。 –

+0

@DarkcatStudios我剛剛看到一個我修正的拼寫錯誤 - 我輸入了一個'='而不是'.'。 –

1
"SELECT [job_ID], 
     [job_ref], 
     [job_title], 
     [job_description], 
     [job_location] 
FROM [Web_Xtr_HR] 
     INNER JOIN [Web_Xtr_Job_Locations] 
      ON [Web_Xtr_HR].job.Location_ID = [Web_Xtr_HR] 
WHERE (
      ([job_ext_int_both] = 1) 
      AND ([job_location] = " + theIndex + ") 
     ) 
ORDER BY [job_ID] DESC" 
0

感謝球員,有點混搭關於我的後結束:

SELECT 
[Web_Xtr_HR].[job_ID], 
[Web_Xtr_HR].[job_ref], 
[Web_Xtr_HR].[job_title], 
[Web_Xtr_HR].[job_description], 
[Web_Xtr_HR].[job_location], 
[Web_Xtr_Job_Locations].[job_location_name], 
[Web_Xtr_Job_Locations].[job_location_country] 
FROM 
[Web_Xtr_HR] 
    INNER JOIN [Web_Xtr_Job_Locations] 
    ON [Web_Xtr_HR].[job_Location] = [Web_Xtr_Job_Locations].[job_Location_ID] 
WHERE 
(([Web_Xtr_HR].[job_ext_int_both] = 1) 
AND 
([Web_Xtr_HR].[job_location] = " + theIndex + ")) 

ORDER BY [Web_Xtr_HR].[job_ID] DESC 
相關問題