2016-10-04 131 views
0

如何檢索我想用連接編寫嵌套查詢的數據? 我想從t1顯示first_name,designation,並且顯示project_namecol2來自t2,其中end_date= (select max(end_date) from project where res_id=?);加入嵌套查詢

能否請你幫我寫SQL查詢

select R.first_name, R.Designation, R.DOB, R.DOJ, R.Department, 
R.city, p.project_name, p.start_date, p.end_date, 
p.end_date+1 as next_avail_date 
from resources R full JOIN project p on (r.res_id=p.res_id) 
where r.u_id='&u_id'; 

我想選擇基於以下查詢

select project_name from project where end_date= (select max(end_date) from project where res_id=1); 

如何添加它在上面的查詢項目名稱?

+0

請顯示您的代碼。 –

+0

select R.first_name,R.Designation,R.DOB,R.DOJ,R.Department, R.city,p.project_name,p.start_date,p.end_date, p.end_date + 1 as next_avail_date from resources R (r.res_id = p.res_id)其中r.u_id ='&u_id';我想從項目中選擇project_name,其中end_date =(從res_id = 1的項目中選擇max(end_date)); –

回答

0

您可以使用子查詢作爲FROM CLAUSE的視圖,並以與表格相同的方式加入。嘗試這樣的事情

select R.first_name, 
     R.Designation, 
     R.DOB, R.DOJ, 
     R.Department, 
     R.city,  
     p.project_name, 
     p.start_date, 
     p.end_date, 
     p.end_date+1 as next_avail_date 
    from resources r, (select project_name 
         from project 
         where end_date= (select max(end_date) 
             from project pr 
             where pr.res_id=1)) p 
where r.u_id='&u_id' 
    and r.res_id=p.res_id; 
+0

我得到這個錯誤ORA-00904:「P」。「RES_ID」:無效標識符 00904. 00000 - 「%s:無效標識符」 *原因: *操作: 錯誤在行:15列:17 –