求一条sql语句

jianjialin 2009-06-17 08:03:30
先有一张论坛信息表

[Code=SQL]
declare @message table(
messageid int identity(1,1) primary key,
topicid int,--外键 主题表ID
Replyto int,--外键 userid,如果该条记录为主题(即论坛的LZ帖,第一条帖那为NULL,如果是跟帖,则保存LZ的ID)
userid int, --本条信息发出者
posted datetime--发布时间
)
[/Code]

想求当前用户的回复过的帖子的主题ID 既topicid 按posted desc排序,去除多余的
...全文
19 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
字云逸 2009-06-18
  • 打赏
  • 举报
回复
既如此,楼主可以试试这个:
select distinct topicid 
from @message
group by topicid
having count(*) >= 1 and Userid='当前用户ID' and Replyto is not null
order by posted desc


因为没有表,所以不知道对不对.楼主可以把带数据表的代码发出来,大家都可以在自己的机器上试下,有了验证结果就可靠了.
仙道彰 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 l1ka1lz8 的回复:]
SQL code[Quote=引用 5 楼 jianjialin 的回复:]
不好意思 刚刚下班走的急 没有说清楚。


SQL code
declare @message table(
messageid int identity(1,1) primary key,
topicid int,--外键 主题表ID
Replyto int,--外键 userid(就是帖子发出人的ID。 如果是发帖 ,那么这为NULL; 如果是跟帖, 则为发帖者的ID)
userid int, --本条信息发出者(跟帖者ID 或者发帖者ID)
posted datetime--发…
[/Quote]

是呀,他和我烦的错误是一样的,呵呵期待牛人的回答
jianjialin 2009-06-18
  • 打赏
  • 举报
回复
回LS的

如果指定了 SELECT DISTINCT,那么 ORDER BY 子句中的项就必须出现在选择列表中。
l1ka1lz8 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jianjialin 的回复:]
不好意思 刚刚下班走的急 没有说清楚。


SQL code
declare @message table(
messageid int identity(1,1) primary key,
topicid int,--外键 主题表ID
Replyto int,--外键 userid(就是帖子发出人的ID。 如果是发帖 ,那么这为NULL; 如果是跟帖, 则为发帖者的ID)
userid int, --本条信息发出者(跟帖者ID 或者发帖者ID)
posted datetime--发布时间
)




想求当前用户的回复过的帖子的…
[/Quote]
select distinct topicid from @message where Userid='当前用户ID' and Replyto is not null --Replayto isnotnull则表示我回帖
order by posted desc

? 这不就不重复了吗?
jianjialin 2009-06-18
  • 打赏
  • 举报
回复
如果指定了 SELECT DISTINCT,那么 ORDER BY 子句中的项就必须出现在选择列表中。

这个我也做过
sid1001 2009-06-18
  • 打赏
  • 举报
回复
select topicid from @message where Userid='userid' and Replyto is not null --Replayto isnotnull则表示我回帖
group by topicid order by posted desc;

这样可以去除多余的

字云逸 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jianjialin 的回复:]
不好意思 刚刚下班走的急 没有说清楚。


SQL code
declare @message table(
messageid int identity(1,1) primary key,
topicid int,--外键 主题表ID
Replyto int,--外键 userid(就是帖子发出人的ID。 如果是发帖 ,那么这为NULL; 如果是跟帖, 则为发帖者的ID)
userid int, --本条信息发出者(跟帖者ID 或者发帖者ID)
posted datetime--发布时间
)




想求当前用户的回复过的帖子的…
[/Quote]

要不重复就加个约束条件:DISTINCT
select distinct topicid from @message where Userid='当前用户ID' and Replyto is not null --Replayto isnotnull则表示我回帖
--distinct 用于去掉结果中的重复行
order by posted desc
jianjialin 2009-06-18
  • 打赏
  • 举报
回复
LS、、、、

posted字段没包含在group by 中或者聚合函数里面啊

一看就通过不了额
仙道彰 2009-06-18
  • 打赏
  • 举报
回复
上面的不对,不好意思
仙道彰 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jianjialin 的回复:]
不好意思 刚刚下班走的急 没有说清楚。


SQL code
declare @message table(
messageid int identity(1,1) primary key,
topicid int,--外键 主题表ID
Replyto int,--外键 userid(就是帖子发出人的ID。 如果是发帖 ,那么这为NULL; 如果是跟帖, 则为发帖者的ID)
userid int, --本条信息发出者(跟帖者ID 或者发帖者ID)
posted datetime--发布时间
)




想求当前用户的回复过的帖子的…
[/Quote]
加个group by

select topicid from @message where Userid='当前用户ID' and Replyto is not null --Replayto isnotnull则表示我回帖
group by topicid order by posted desc
jianjialin 2009-06-17
  • 打赏
  • 举报
回复
不好意思 刚刚下班走的急 没有说清楚。

[Code=SQL]
declare @message table(
messageid int identity(1,1) primary key,
topicid int,--外键 主题表ID
Replyto int,--外键 userid(就是帖子发出人的ID。 如果是发帖 ,那么这为NULL; 如果是跟帖, 则为发帖者的ID)
userid int, --本条信息发出者(跟帖者ID 或者发帖者ID)
posted datetime--发布时间
)

[/Code]
想求当前用户的回复过的帖子的主题ID(topicid)
按posted desc排序
我的答案是
[Code=SQL]
select topicid from @message where Userid='当前用户ID' and Replyto is not null --Replayto isnotnull则表示我回帖
order by posted desc[/Code]

得到当前用户回过贴的 帖子 的id(topicid)

但是,一个帖子可以回复很多次,因此,topicid会重复。
我想要一个不重复我结果。 但是试了好多次,不成功。
可能写了一天的代码累了。


Tomzzu 2009-06-17
  • 打赏
  • 举报
回复

select topicid from @message where Replyto = '当前登录ID' order by posted desc
--小F-- 2009-06-17
  • 打赏
  • 举报
回复
select messageid 
from @message
where Replyto = '当前登录ID'
order by posted desc
ks_reny 2009-06-17
  • 打赏
  • 举报
回复
不懂意思。既topicid 按posted desc排序是什么意思?
you_tube 2009-06-17
  • 打赏
  • 举报
回复
select messageid ,
topicid ,
Replyto ,
userid ,
posted
from Tab
where Replyto = '当前登录ID'
order by posted desc

?

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧