简体   繁体   中英

Mysql - How to select entries from one table based on whether or not I subscribe to them from another table?

For the life of me I cannot wrap my head around this simple mysql problem. (my brain is fried, almost 16 hours of straight coding).

I have, essentially, the tables entries and subscriptions

entries

  • publisherId
  • entry

subscriptions

  • myId
  • friendId

I want to be able to select all the entries that are published by all the people I am subscribed to in subscriptions .

I would love if it would be possible to do this with codeigniter's activerecord syntax as well, but if it's impossible, plain mysql syntax would be so greatly appreciated.

What concept am I missing here? Is it a simple join or is it a matter of looping through all the people I subscribe to and finding entries that are published by them?

Thank you so much.

You can use a JOIN. In plain SQL:

SELECT e.publisherId, e.entry
FROM entries e
JOIN subscriptions s
ON s.friendId = e.publisherId
WHERE s.myId = 1234

let me know if it works , because I am still learning Active Query records (join is confusing to me :P )

$this->db->select('entries.publisherId,entries.entry');
$this->db->from('entries');
$this->db->join('comments', 'subscriptions.friendId = entries.publisherId');
$this->db->where('subscriptions.myId', 1234); 
$query = $this->db->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM