#1066 - 非唯一表/别名:'考试'
问题描述:
我想在一个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
答
您列出表检查两次,但你没有给它一个别名。 一旦进入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
内部连接与'考试'并再次'连接'与'考试'?不知道如何在多次加入同一个表格时需要提供唯一的别名。 'date,value和tname'来自哪个表? – 2014-11-21 19:50:25