在 codeigniter 中查询:get where or [英] query in codeigniter: get where or

查看:65
本文介绍了在 codeigniter 中查询:get where or的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择多个或在 codeigniter 中?

how can I select multiple or in codeigniter?

例如,select * from bla where id = 1 or id = 2 or id = 3?

如果我使用这个:

$this->db->where('name', $name);
$this->db->where('title', $title);
$this->db->where('status', $status); 

它与 AND...

推荐答案

您可以使用 where_in 方法作为同一列的多个 or 语句的快捷方式:

You can use the where_in method as a shortcut to multiple or-statements for the same column:

$available_ids = [1, 2, 3];

$this->db->where_in('id', $available_ids);
// WHERE id IN (1, 2, 3)

如果您想检查多个列(名称为Adam"或标题为Grand Poobah"或状态为Active"),您可以改用 or_where 方法:

If you were looking to check multiple columns (the name is 'Adam' or the title is 'Grand Poobah' or the status is 'Active'), you can use the or_where method instead:

$this->db->where('name', $name);
$this->db->or_where('title', $title);
$this->db->or_where('status', $status); 
// WHERE name = 'Adam' OR title = 'Grand Poobah' OR status = 'Active'

把它们放在一起,你会

$available_ids = [1, 2, 3];

$query = $this->db->select('*')->from('bla')->where_in('id', $available_ids)->get();
// SELECT * FROM bla WHERE id IN (1, 2, 3)

CodeIgniter v3 参考
CodeIgniter v2 参考

这篇关于在 codeigniter 中查询:get where or的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆