sql 帮助从所有关注者获取数据? (如果我们关注的话就像推特一样)

发布于 2024-09-16 14:37:11 字数 2007 浏览 5 评论 0原文

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

疑问

如何从我的公司 (这里的例子是 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 。 (西德 = 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 )) 

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

alt text

example if i have

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

question

how do i get all the feeds.description from the company that my company ( example here is cid = 1 ) ?

heres my simple pdo sql to get just mine.

    $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

maybe something like this ? ( fail )

    $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

or simpler get feeds.description from each follow.following ( cid = 1,2,3,etc ) from me. ( cid = 1)
or if twitter get all my friends status ( friends that i follow )

edit*

some good guys at irc mysql said to use joins. but i just dont get it.
what i get is this

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

then

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

anyone can give me an example for joins in this problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二货你真萌 2024-09-23 14:37:11

因此,假设问题是“我如何从我的公司正在关注的公司获取所有 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) 关注的所有公司。

编辑: Feed 应包括我公司的 Feed 以及我公司正在关注的 Feed。

So, assuming that the question is "How do i get all the feeds.description from the company that my company IS FOLLOWING?" you want to join using the following key:

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;

This should list all of the companies that your :cid (1) is following.

EDIT: Feeds should include my company's feeds + feeds my company is following.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文