ORA-00918: 未明确定义列
1.SELECT T1.ID, T2.NAME FROM T1, T2
2 WHERE T1.ID = T2.ID
3 AND T1.NAME = 'A'
4 AND FLAG = '1';
AND FLAG = '1'
*第 4 行出现错误:
ORA-00918: 未明确定义列
现在问题出现了,T2表新增的字段与T1的FLAG字段同名,则联合查询T1和T2表时,没有明确指定表信息的FLAG列将会导致SQL分析错误。
这个问题比较隐蔽,不过在书写SQL的时候依照良好的SQL编码规范,还是可以避免问题的方式的。
应该定义规范:在多表查询时,字段一定需要指定表。
即:t1.XX;t2.XX
下面的使用 t.* 的这种情况,也很容易出现 ORA-00918: 未明确定义列 这样的异常,
sql="select * from (select t.* ,c.column_id,d.level_code from cms_article t ,cms_column_article c ,CMS_COLUMN d where t.id=c.article_id and c.column_id=d.id and d.LEVEL_CODE like'"+levelCode+"%' and t.id <>'"+id+"' and t.state='已发布' order by t.createtime desc) where rownum <3";
解决的方法,就是在sql中不使用 * ,并且 * 也会影响sql的 执行效率,把要查询的列都列全
sql="select * from (select t.title ,t.id ,c.column_id,d.level_code,d.form_id from cms_article t ,cms_column_article c ,CMS_COLUMN d,cms_form f where t.id=c.article_id and c.column_id=d.id and d.LEVEL_CODE like'"+code+"%' and t.state='已发布' and d.form_id=f.id order by t.createtime desc) where rownum <6";