【问题标题】:Mysql - How to select entries from one table based on whether or not I subscribe to them from another table?Mysql - 如何根据我是否从另一个表中订阅它们来从一个表中选择条目?
【发布时间】:2011-03-20 08:19:52
【问题描述】:

对于我的一生,我无法解决这个简单的 mysql 问题。 (我的大脑被炸了,差不多 16 个小时的直接编码)。

基本上,我有 entriessubscriptions

参赛作品

  • 发布者 ID
  • 条目

订阅

  • 我的身份证
  • 朋友ID

我希望能够选择我在订阅中订阅的所有人发布的所有条目。

如果也可以使用 codeigniter 的 activerecord 语法来做到这一点,我会很高兴,但如果不可能,那么简单的 mysql 语法将不胜感激。

我在这里缺少什么概念?是简单的加入还是循环遍历我订阅的所有人并找到他们发布的条目?

非常感谢。

【问题讨论】:

    标签: php mysql codeigniter logic


    【解决方案1】:

    您可以使用 JOIN。在纯 SQL 中:

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

    【讨论】:

      【解决方案2】:

      让我知道它是否有效,因为我仍在学习 Active Query 记录(加入让我感到困惑: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();
      

      【讨论】:

      • 您好,非常接近,但看起来我不得不稍微更改第三行,非常感谢! $this->db->select('entries.publisherId,entries.entry'); $this->db->from('entries'); $this->db->join('subscriptions','subscriptions.friendId = entries.userId'); $this->db->where('subscriptions.userId', 1234);
      猜你喜欢
      • 1970-01-01
      • 2012-03-23
      • 2017-02-11
      • 2020-07-28
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多