注册 登录
编程论坛 SQL Server论坛

求一段sql语句

tokoyoshi 发布于 2009-11-18 11:05, 780 次点击
我想查询,一篇新闻和对应它的所有评论,我这样写

create proc com_infor
    @id int=0
    as
    begin
    select * from CommentaryInfo inner join InformationInfo on InformationInfo.ifid=@id
    end
    go

但是查询出来的是
1    aaaa    aaa   1    1  (<--评论)  1    aaa    aaaaaaaaaaaaaaaaaaaaa    NULL    1990-12-12 00:00:00.000    1(新闻)
2    bbb    bbbb    1    1    (<--评论)1    aaa    aaaaaaaaaaaaaaaaaaaa    NULL    1990-12-12 00:00:00.000    1(新闻)

也就是 1个新闻,两条对应的评论,结果每条评论都对应一次新闻,我想只有一条新闻,应该怎么写,最好能稍微解释下,谢谢


[ 本帖最后由 tokoyoshi 于 2009-11-18 11:07 编辑 ]
3 回复
#2
师妃暄2009-11-18 12:18
可不能inner啊,要left join
#3
tokoyoshi2009-11-18 14:05
效果是一样的·没差别·····
#4
uubird2009-11-23 21:55
程序没有错!!你自己写的也没错。。放在数据库里是这个格式的。。。
create proc com_infor
    @id int=0
    as
    begin
    select * from CommentaryInfo inner join InformationInfo on InformationInfo.ifid=CommentaryInfo.id
    where InformationInfo.ifid=@id
    end
    go
你好像网了加限制。。。
照你的写法查询的结果直接产生的是笛卡尔集。。。
希望我没记错。。呵呵
1