当前位置: 代码迷 >> Sql Server >> 联接表出现不明确,该怎么处理
  详细解决方案

联接表出现不明确,该怎么处理

热度:28   发布时间:2016-04-27 20:27:53.0
联接表出现不明确
原来的语句正常:


select   top     10   topicid,title,boardid,postusername,lastpost,lastposttime,dateandtime   from   dv_topic   where   Not   Exists(Select   dateandtime   From   dv_topic   Where   boardid   =   A.boardid   And   dateandtime   >   A.dateandtime)   and   boardid   not   in(69,97,107,444,777)   order   by   dateandtime   desc


后来欲增加连接查询一个相关联的子段.
dv_board表的BoardName,
连接条件是dv_topic表的boardid等于dv_board表的boardid

select   top     10   a.topicid,a.title,a.boardid,a.postusername,a.lastpost,a.lastposttime,a.dateandtime,b.BoardName   from   dv_topic   A   Inner   join   dv_board   b   on   a.boardid=b.boardid
where   Not   Exists(Select   dateandtime   From   dv_topic   Where   boardid   =   A.boardid   And   dateandtime   >   A.dateandtime)   and   boardid   not   in(69,97,107,444,777)   order   by   dateandtime   desc

==
查询出现
Microsoft   OLE   DB   Provider   for   SQL   Server   错误   '80040e14 '  

列名   'boardid '   不明确。  
估计是
select   top     10   a.topicid,a.title,a.boardid,a.postusername,a.lastpost,a.lastposttime,a.dateandtime,b.BoardName   from   dv_topic   A   Inner   join   dv_board   b   on   a.boardid=b.boardid
where   Not   Exists(Select   dateandtime   From   dv_topic   Where   boardid   =   A.boardid   And   dateandtime   >   A.dateandtime)   and   boardid   not   in(69,97,107,444,777)   order   by   dateandtime   desc

这一段里的后面部分有错误.只运行前部分不会出错.
where   Not   Exists(Select   dateandtime   From   dv_topic   Where   boardid   =   A.boardid

这里应改成什么??

------解决方案--------------------
select top 10 a.topicid,a.title,a.boardid,a.postusername,a.lastpost,a.lastposttime,a.dateandtime,b.BoardName from dv_topic A Inner join dv_board b on a.boardid=b.boardid
where Not Exists(Select dateandtime From dv_topic Where boardid = A.boardid And dateandtime > A.dateandtime) and boardid not in(69,97,107,444,777) order by dateandtime desc

--这个不明确:boardid 包括(A.和B.)
A.boardid not in(69,97,107,444,777) order by dateandtime desc
B.boardid not in(69,97,107,444,777) order by dateandtime desc
------解决方案--------------------
对于两个表中都有的字段名需要指定属于哪个表。
------解决方案--------------------
碰到这种情况直接用:表名.字段 表示
------解决方案--------------------
hehe,发完就解决了
------解决方案--------------------
在前面把 这个字段的所属表名加上就可以了!
  相关解决方案