【问题标题】:sql help getting data from all the followers ? ( like twitter if we follow )sql 帮助从所有关注者那里获取数据? (喜欢推特,如果我们关注)
【发布时间】:2010-08-29 22:04:15
【问题描述】:

如果我有示例

follow table
company one ( cid = 1 ) following two ( cid = 2 )
company one ( cid = 1 ) following three( cid = 3 )

feeds table
company one ( cid = 1 ) type 'product' description 'hello ive updated a product';
company two ( cid = 2 ) type 'product' description 'hello ive updated a product im from company 2';
company three ( cid = 3 ) type 'shoutout' description 'hello ive i got a shoutout im company 3';
company one ( cid = 1 ) type 'product' description 'hello ive updated my second product';

问题

我如何从我的公司获得所有 feeds.description(这里的示例是 cid = 1)?

这是我的简单 pdo sql 来获取我的。

    $data['feeds']['cid'] = 1;

    return $this->db->fetchAll("SELECT feeds.type, feeds.$type, feeds.cid, feeds.time, companies.name FROM feeds, companies WHERE 
    feeds.cid = :cid AND companies.cid = :cid ORDER BY feeds.fid DESC LIMIT 0, 5", $data['feeds']);

this will display
hello ive updated a product
hello ive updated my second product

也许是这样的? (失败)

    $data['feeds']['cid'] = 1,2,3;

    return $this->db->fetchAll("SELECT feeds.type, feeds.$type, feeds.cid, feeds.time, companies.name FROM feeds, companies WHERE 
    feeds.cid = :cid AND companies.cid = :cid ORDER BY feeds.fid DESC LIMIT 0, 5", $data['feeds']);

this should be displaying like
hello ive updated a product
hello ive updated a product im from company 2
hello ive i got a shoutout im company 3
hello ive updated my second product

或更简单地从我的每个 follow.following ( cid = 1,2,3,etc ) 中获取 feeds.description。 (cid = 1) 或者,如果 twitter 获得了我所有朋友的状态(我关注的朋友)

编辑*

irc mysql 的一些好人说要使用连接。但我就是不明白。 我得到的是这个

fetch all the follow.following from cid = me ( example cid = 1 )

然后

SELECT * FROM feeds WHERE feeds.cid = IN (2,3,cid that im following ( see follow.following )) 

任何人都可以给我一个连接这个问题的例子吗?

【问题讨论】:

  • $type的值是多少?
  • ups 顺便说一句,提要中有一段时间。不知何故,图像有些错误:D。 type 的值是信息的类型,比如这是一个 'shoutout' 这是一个 'updatestatus' 等等,所以我们以后可以很容易地管理它们

标签: php sql mysql pdo php-5.3


【解决方案1】:

因此,假设问题是“我如何从我的公司关注的公司获取所有 feeds.description?”您想使用以下密钥加入:

select 
companies.name, 
feeds.type, 
feeds.description 
from follow 
inner join feeds on (follow.following = feeds.cid or follow.cid = feeds.cid)
inner join companies on (feeds.cid = companies.cid)
where follow.cid = :cid
order by feeds.fid desc 
limit 5;

这应该列出您的 :cid (1) 关注的所有公司。

编辑:提要应包括我公司的提要 + 我公司关注的提要。

【讨论】:

  • 但还有一个问题,我们自己的提要在哪里?
  • 所以您正在寻找自己公司的提要 + 贵公司关注的提要?
  • 是的!顺便说一句,不错。如果可以的话,我可以给你+50 :|但我必须等待 2 天。无论如何,有人说我的数据库场景无法扩展。
  • 顺便说一句...没问题。你发布了一个 ERD,你应该得到一个答案。
  • 不,以下列未根据您的 ERD 建立索引... alter table follow 添加外键(以下)引用公司(CID);
猜你喜欢
  • 2016-01-06
  • 2016-04-07
  • 2011-09-20
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多