doucheng8471 2017-09-06 19:08
浏览 79

Codeigniter查询生成器 - 需要检查条目是否存在于多列中

We have a table with multiple attributes, such as:

TABLE_ATTRIBUTES
ID,   Color, Size, Weight
111, Red,   XL     null
112, null,    SM    10

I want to build a method inside my model, that returns the row ID, by checking all the columns for any attributes. For example, if I want to pass 'green', 'SM', '9'.

What's the best way to build this Codeigniter DB Query?

I have something like this, but it's not working:

$this->db->select('id');
->or_where($color === NULL ? 'color IS NOT NULL' : 'color =', $color)
->or_where($size === NULL ? 'size IS NOT NULL' : 'size =', $size)
->or_where($weight === NULL ? 'weight IS NOT NULL' : 'weight =', $weight_10)
$row = $this->db->get('product')->row();
  • 写回答

1条回答 默认 最新

  • 普通网友 2017-09-06 23:01
    关注

    Your first or_where should just be where according to the documentation so you probably want to alter it slightly:

    $this->db->select('id');
    ->where($color === NULL ? 'color IS NOT NULL' : 'color =', $color)
    ->or_where($size === NULL ? 'size IS NOT NULL' : 'size =', $size)
    ->or_where($weight === NULL ? 'weight IS NOT NULL' : 'weight =', $weight_10)
    $row = $this->db->get('product')->row();
    

    See this doc about $this->db->or_where() for their example: https://www.codeigniter.com/userguide3/database/query_builder.html

    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程