数据库查询,三张表

longforblossom 2010-08-27 02:26:35
tb_log(日志表)
userId,log

tb_friend (朋友表)
userid,friendid

tb_attention (关注表)
userid,touserid

根据一个用户ID得到朋友和关注人的日志
怎么查询合适?
...全文
123 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
rdcxjn 2010-08-27
  • 打赏
  • 举报
回复
SELECT l.log
FROM
(
SELECT f.friendid id
FROM tb_friend f
WHERE f.userid = tmpuserid/*tmpuserid是你要为其查找朋友及关注人的userid*/
UNION
SELECT a.touserid id
FROM tb_attention a
WHERE a.userid = tmpuserid/*tmpuserid是你要为其查找朋友及关注人的userid*/
)tb_id,
tb_log l
WHERE tb_id.id = l.userid
WXHDSWH 2010-08-27
  • 打赏
  • 举报
回复

[Quote=引用 6 楼 studentaccp 的回复:]
引用 2 楼 vber1010 的回复:
select log from tb_log where userId in (select friendid from tb_friend where userid=&id union all select touserid from tb_attention where userid=&id)

这个不错 啊,把i……
[/Quote]
支持下
studentaccp 2010-08-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 vber1010 的回复:]
select log from tb_log where userId in (select friendid from tb_friend where userid=&id union all select touserid from tb_attention where userid=&id)
[/Quote]
这个不错 啊,把in改成exists就更好了,再给列都起别名以减少解析时间。
longforblossom 2010-08-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 vber1010 的回复:]

select log from tb_log where userId in (select friendid from tb_friend where userid=&id union all select touserid from tb_attention where userid=&id)
[/Quote]

这个比我的那个要快一些 还有更好的没?
csuxp2008 2010-08-27
  • 打赏
  • 举报
回复
tb_friend与tb_friend自连一次,然后再去连接tb_log和tb_attention
vber1010 2010-08-27
  • 打赏
  • 举报
回复
select log from tb_log where userId in (select friendid from tb_friend where userid=&id union all select touserid from tb_attention where userid=&id)
kingkingzhu 2010-08-27
  • 打赏
  • 举报
回复
关联 不可避免

17,078

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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