【问题标题】:codeigniter activerecords left join not behaving as expectedcodeigniter activerecords left join 未按预期运行
【发布时间】:2013-05-04 01:14:06
【问题描述】:

我有两个如下所示的表:

Table_one


P_id     Value
1001     50
1002     54
1003     47
1004     65
1005     51

Table_two

S_id     Rating     t_id
1001     1          1
1002     3          1
1001     5          2
1004     1          2
1005     2          2

我正在尝试使用 codeigniter 的活动记录类连接这两个表。这是我的代码:

$tId = 1;
$this->db->select('Table_one.Value, Table_two.Rating');
$this->db->from('Table_one');
$this->db->join('Table_two', 'Table_one.P_id = Table_two.S_id', 'left');
$this->db->where('Table_two.t_id', $tId);
$query = $this->db->get();

我希望此查询能够提供表 1 中的所有 5 行数据,并将表 2 的“评级”列中的值附加到前两行。相反,我只从前两行获取数据。如何重写代码,继续使用活动记录类,以获得预期的结果?

【问题讨论】:

    标签: php codeigniter activerecord


    【解决方案1】:

    您的 $tid 在 where 子句中似乎是 1。这将只检索前两行的值。

    【讨论】:

      【解决方案2】:

      如果你想得到所有 5 行,你需要去掉 where 子句,因为 Table_two 中只有两行 t_id = 1,这就是你得到的。

      【讨论】:

        【解决方案3】:

        通过查看您的代码,它必须返回 2 行...如果您想检索 5 行然后删除 where 子句。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-16
          • 2019-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多