简体   繁体   中英

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. 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); the result shows only the officers with p_code=10.

How can I add these two lines with where clause in my code ? Can anyone help ?

Use or_where , so you can get both or one of them:

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

And you can use where_in too:

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

You need to use or_where to join both statements, like so:

$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.

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