简体   繁体   English

具有两个条件的数据表 Where 子句

[英]Datatables Where clause with two conditions

I want to select records where p_code=10 & p_code=24 only for the user = 4, in my MySQL Datatables view.我想在我的 MySQL 数据表视图中为用户 = 4 选择 p_code=10 & p_code=24 的记录。 I used the following code fragment.我使用了以下代码片段。

$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);
        }

But the code outs an empty result.但是代码输出了一个空结果。 If I remove the line, $this->datatables->where('tbl_officer.p_code', 24);如果我删除该行, $this->datatables->where('tbl_officer.p_code', 24); the result shows only the officers with p_code=10.结果仅显示 p_code=10 的军官。

How can I add these two lines with where clause in my code ?如何在我的代码中使用 where 子句添加这两行? Can anyone help ?任何人都可以帮忙吗?

Use or_where , so you can get both or one of them:使用or_where ,这样您就可以获得两者或其中之一:

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

And you can use where_in too:你也可以使用where_in

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

You need to use or_where to join both statements, like so:您需要使用or_where来连接这两个语句,如下所示:

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

Without it you will just run two separate queries (one for p_code == 10 and other for p_code == 24 ) and return the value of the last statement.没有它,您将只运行两个单独的查询(一个用于p_code == 10 ,另一个用于p_code == 24 )并返回最后一条语句的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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