2014-11-21 55 views
0

我想在一個SQL語句中獲取考試日期,測試和測試名稱的值。我能夠分兩個階段完成,但不能完成一個階段。當我嘗試合併它時,我會在標題中發現錯誤。#1066 - 非唯一表/別名:'考試'

這裏是SQL語句產生錯誤:

select date, value, tname from examination, testresults, testname 
LEFT JOIN examtype ON examtype.etype_id = examination.etype_id 
LEFT JOIN examination ON examination.examination_id = testresults.examination_id 
LEFT JOIN testname ON testname.tname_id = testresults.tname_id 
where examination.patientnhs_no= '1001001002' 
    and date > '2008/09/05' 
    and examtype.name like 'blood%' 
    and testname.name like'tsh%' 
order by date asc 
limit 1 
+0

內部連接與'考試'並再次'連接'與'考試'?不知道如何在多次加入同一個表格時需要提供唯一的別名。 'date,value和tname'來自哪個表? – 2014-11-21 19:50:25

回答

0

您列出表檢查兩次,但你沒有給它一個別名。 一旦進入FROM子句並且一旦進入LEFT JOIN。 您probally彪爲

LEFT JOIN examination 

LEFT JOIN testresults 

與FROM後取出testresults和測試名稱。 (見這link)。

以下應該工作(我還沒有測試過)。

select date, value, tname from examination 
       LEFT JOIN examtype ON examtype.etype_id = examination.etype_id 
       LEFT JOIN testresults ON examination.examination_id = testresults.examination_id 
       LEFT JOIN testname ON testname.tname_id = testresults.tname_id 
       where examination.patientnhs_no= '1001001002' and date > '2008/09/05' and examtype.name like 'blood%' and testname.name like'tsh%' order by date asc limit 1 
相關問題