我们有一个包含多个属性的表,例如:
TABLE_ATTRIBUTES
ID,颜色,尺寸,重量
111,Red,XL null
112,null,SM 10
我想在我的模型中构建一个方法,通过检查任何属性的所有列来返回行ID。例如,如果我想传递'绿色','SM','9'。
构建此Codeigniter数据库查询的最佳方法是什么?
我有类似的东西,但它不起作用:
$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();
答案 0 :(得分:0)
根据文档,您的第一个A --> B x y | x
B --> C D
C --> A | c
D --> d
应该只是or_where
,所以您可能想稍微改变一下:
where
请参阅此文档,了解$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();
的示例:
https://www.codeigniter.com/userguide3/database/query_builder.html