使用 CodeIgniter 的查询构建方法,具有由 OR 分隔的两个条件的 WHERE 子句

发布于 2024-09-05 01:39:19 字数 284 浏览 12 评论 0原文

我正在使用以下代码从 CodeIgniter Web 应用程序中的 MySQL 数据库中进行选择:

$query = $this->db
    ->get_where('mytable', array('id' => 10));

这非常有效,但我想使用 CI 查询生成器方法编写以下 MySQL 语句。

SELECT *
FROM `mytable`
WHERE `id`='10' OR `field`='value'

I am using the following code to select from a MySQL database in a CodeIgniter webapp:

$query = $this->db
    ->get_where('mytable', array('id' => 10));

This works great, but I want to write the following MySQL statement using CI query builder methods.

SELECT *
FROM `mytable`
WHERE `id`='10' OR `field`='value'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

雪化雨蝶 2024-09-12 01:39:19
$where = "name='Joe' AND status='boss' OR status='active'";

$this->db->where($where);
$where = "name='Joe' AND status='boss' OR status='active'";

$this->db->where($where);
瑕疵 2024-09-12 01:39:19

您可以使用 or_where() 来实现 - CI 文档中的示例:

$this->db->where('name !=', $name);

$this->db->or_where('id >', $id); 

// Produces: WHERE name != 'Joe' OR id > 50

You can use or_where() for that - example from the CI docs:

$this->db->where('name !=', $name);

$this->db->or_where('id >', $id); 

// Produces: WHERE name != 'Joe' OR id > 50
鹿! 2024-09-12 01:39:19

你可以使用这个:

$this->db->select('*');
$this->db->from('mytable');
$this->db->where(name,'Joe');
$bind = array('boss', 'active');
$this->db->where_in('status', $bind);

You can use this :

$this->db->select('*');
$this->db->from('mytable');
$this->db->where(name,'Joe');
$bind = array('boss', 'active');
$this->db->where_in('status', $bind);
冷月断魂刀 2024-09-12 01:39:19

将使用活动记录方法or_where

$this->db->select("*")
->from("table_name")
->where("first", $first)
->or_where("second", $second);

Active record method or_where is to be used:

$this->db->select("*")
->from("table_name")
->where("first", $first)
->or_where("second", $second);
海未深 2024-09-12 01:39:19
$where = "name='Joe' AND status='boss' OR status='active'";

$this->db->where($where);

虽然我迟到了 3/4 个月,但在定义了 where 子句后,您仍然执行以下操作... $this->db->get("tbl_name");

$where = "name='Joe' AND status='boss' OR status='active'";

$this->db->where($where);

Though I am 3/4 of a month late, you still execute the following after your where clauses are defined... $this->db->get("tbl_name");

白云悠悠 2024-09-12 01:39:19

对我有用的:

  $where = '';
   /* $this->db->like('ust.title',$query_data['search'])
        ->or_like('usr.f_name',$query_data['search'])
        ->or_like('usr.l_name',$query_data['search']);*/
        $where .= "(ust.title like '%".$query_data['search']."%'";
        $where .= " or usr.f_name like '%".$query_data['search']."%'";
        $where .= "or usr.l_name like '%".$query_data['search']."%')";
        $this->db->where($where);



$datas = $this->db->join(TBL_USERS.' AS usr','ust.user_id=usr.id')
            ->where_in('ust.id', $blog_list) 
            ->select('ust.*,usr.f_name as f_name,usr.email as email,usr.avatar as avatar, usr.sex as sex')
            ->get_where(TBL_GURU_BLOG.' AS ust',[
                'ust.deleted_at'     =>  NULL,
                'ust.status'     =>  1,
            ]); 

我必须这样做才能创建如下查询:

SELECT `ust`.*, `usr`.`f_name` as `f_name`, `usr`.`email` as `email`, `usr`.`avatar` as `avatar`, `usr`.`sex` as `sex` FROM `blog` AS `ust` JOIN `users` AS `usr` ON `ust`.`user_id`=`usr`.`id` WHERE (`ust`.`title` LIKE '%mer%' ESCAPE '!' OR  `usr`.`f_name` LIKE '%lok%' ESCAPE '!' OR  `usr`.`l_name` LIKE '%mer%' ESCAPE '!') AND `ust`.`id` IN('36', '37', '38') AND `ust`.`deleted_at` IS NULL AND `ust`.`status` = 1 ;

What worked for me :

  $where = '';
   /* $this->db->like('ust.title',$query_data['search'])
        ->or_like('usr.f_name',$query_data['search'])
        ->or_like('usr.l_name',$query_data['search']);*/
        $where .= "(ust.title like '%".$query_data['search']."%'";
        $where .= " or usr.f_name like '%".$query_data['search']."%'";
        $where .= "or usr.l_name like '%".$query_data['search']."%')";
        $this->db->where($where);



$datas = $this->db->join(TBL_USERS.' AS usr','ust.user_id=usr.id')
            ->where_in('ust.id', $blog_list) 
            ->select('ust.*,usr.f_name as f_name,usr.email as email,usr.avatar as avatar, usr.sex as sex')
            ->get_where(TBL_GURU_BLOG.' AS ust',[
                'ust.deleted_at'     =>  NULL,
                'ust.status'     =>  1,
            ]); 

I have to do this to create a query like this :

SELECT `ust`.*, `usr`.`f_name` as `f_name`, `usr`.`email` as `email`, `usr`.`avatar` as `avatar`, `usr`.`sex` as `sex` FROM `blog` AS `ust` JOIN `users` AS `usr` ON `ust`.`user_id`=`usr`.`id` WHERE (`ust`.`title` LIKE '%mer%' ESCAPE '!' OR  `usr`.`f_name` LIKE '%lok%' ESCAPE '!' OR  `usr`.`l_name` LIKE '%mer%' ESCAPE '!') AND `ust`.`id` IN('36', '37', '38') AND `ust`.`deleted_at` IS NULL AND `ust`.`status` = 1 ;
流年里的时光 2024-09-12 01:39:19

您无法完全享受 CodeIgniter 的查询生成器方法并单独使用 get_where() 实现 OR 条件,因为它隐式地使用 AND 连接条件。

or_where() 确实可以方便地接收条件数组并用 OR 连接每个条件。

public function getWhereOr(int $id, string $field): array
{
    return $this->db
        ->or_where(['id' => $id, 'field' => $field])
        ->get('mytable')
        ->result();
}

渲染 SQL:

SELECT *
FROM mytable
WHERE `id` = 10 OR `field` = 'value'

or_where() 的其他实现可以在这里找到:

You cannot fully enjoy CodeIgniter's query builder methods and implement the OR condition with get_where() alone because it implicitly joins conditions with AND.

or_where() does conveniently receive an array of conditions and joins each condition with OR.

public function getWhereOr(int $id, string $field): array
{
    return $this->db
        ->or_where(['id' => $id, 'field' => $field])
        ->get('mytable')
        ->result();
}

Rendered SQL:

SELECT *
FROM mytable
WHERE `id` = 10 OR `field` = 'value'

Other implementations of or_where() can be found here:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文