SQL تساعد في الحصول على بيانات من جميع المتابعين؟ (مثل Twitter إذا تابعنا)
سؤال
مثال إذا كان لدي
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
أو أبسط الحصول على 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 ))
يمكن لأي شخص أن يعطيني مثالاً للوصول في هذه المشكلة؟
المحلول
لذا ، على افتراض أن السؤال هو "كيف يمكنني الحصول على كل الخلاصات. التوصية من الشركة التي تتابعها شركتي؟" تريد الانضمام باستخدام المفتاح التالي:
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).
تعديل: يجب أن تشمل الخلاصات خلاصات شركتي + خلاصات شركتي تتبعها.
لا تنتمي إلى StackOverflow