【问题标题】:Datatables Where clause with two conditions具有两个条件的数据表 Where 子句
【发布时间】:2020-01-08 10:10:39
【问题描述】:

我想在我的 MySQL 数据表视图中为用户 = 4 选择 p_code=10 & p_code=24 的记录。我使用了以下代码片段。

$usr = $this->session->userdata('id_user'); 
if($usr == 4)
        {
        $this->datatables->where('tbl_officer.p_code', 10);
        $this->datatables->where('tbl_officer.p_code', 24);

        } else {
        $this->datatables->where('tbl_officer.usr', $usr);
        }

但是代码输出了一个空结果。如果我删除该行, $this->datatables->where('tbl_officer.p_code', 24);结果仅显示 p_code=10 的官员。

如何在我的代码中添加这两行与 where 子句?有人可以帮忙吗?

【问题讨论】:

    标签: mysql codeigniter


    【解决方案1】:

    使用or_where,这样你就可以获得两者或其中之一:

    $this->datatables->where('tbl_officer.p_code', 10)->or_where('tbl_officer.p_code', 24);
    

    你也可以使用where_in

    $this->datatables->where_in('tbl_officer.p_code', [10, 24]);
    

    【讨论】:

    • 没用过这个但是有whereIn选项吗?
    • @vivek_23 是的。有where_in
    【解决方案2】:

    您需要使用or_where 来连接这两个语句,如下所示:

    $this->datatables->where('tbl_officer.p_code', 10)->or_where('tbl_officer.p_code', 24);
    

    没有它,您将只运行两个单独的查询(一个用于p_code == 10,另一个用于p_code == 24)并返回最后一条语句的值。

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 1970-01-01
      • 2012-01-26
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      相关资源
      最近更新 更多