2011-04-14 99 views
0
select 
s.id, s.description, s.improvement, s.previous_year_id, 
s.current_year_id, s.first_name, s.last_name, s.username, 
s.finding, s.action, s.share, s.learned, s.timestamp, 
d.title as department_title, 
group_concat(g.title SEPARATOR \' | \') as strategic_goals, 
y1.year as current_year_title, y2.year as previous_year_title, 
u.summary_id, u.file_name as file_name 
from 
summary s, year y1, year y2, strategic_goal_entries sge, 
goal g, department d, uploads u 
where 
s.id = sge.summary_id 
and 
s.current_year_id = y1.id 
and 
s.previous_year_id = y2.id 
and 
sge.goal_id = g.id 
and 
s.id = u.summary_id 
and 
s.department_id = d.id 
and 
s.department_id = '4' 
group by 
s.id 

從具有在上傳表中有關記錄包含上傳內的值彙總表(s.id = uploads.summary_id)僅返回記錄.summary_id字段僅返回記錄時s.id = u.summary_id

我想返回所有記錄,不管它是否有與之關聯的文件。

任何幫助表示讚賞。

+0

左加入上傳表.... – 2011-04-14 18:33:21

回答

2

建議重構此SQL查詢以使用ANSI連接。爲了實現你的目標,你需要一個左連接代替:

SELECT /*your columns*/ 
from summary s 
INNER JOIN year y1 ON s.current_year_id = y1.id 
INNER JOIN year y2 ON s.previous_year_id = y2.id 
INNER JOIN strategic_goal_entries sge ON s.id = sge.summary_id 
INNER JOIN goal g ON sge.goal_id = g.id 
INNER JOIN department d ON s.department_id = d.id 
LEFT JOIN uploads u ON s.id = u.summary_id 

WHERE s.department_id = '4' 
group by s.id 
相關問題