alt text

例如,如果我有

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';

问题

我如何获取所有公司的所有提要。

这是我简单的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

或更简单地获取feed。从我的每一个下文中的描述。从我这里遵循(cid = 1,2,3等)。 (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 )) 

有人可以给我一个加入这个问题的例子吗?

有帮助吗?

解决方案

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

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)所关注的所有公司。

编辑: 提要应包括我公司的饲料 +我的公司所关注的供稿。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top