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

时间:2020-01-08 10:10:39

标签: mysql codeigniter

我想在我的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_nzl.p_code',24);结果仅显示p_code = 10的军官。

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

2 个答案:

答案 0 :(得分:3)

使用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]);

答案 1 :(得分:0)

您需要使用or_where来连接两个语句,就像这样:

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

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

相关问题