【问题标题】:select from table or where within and operator in codeigniter从表中选择或在 codeigniter 中的 where 和运算符中选择
【发布时间】:2014-08-07 16:18:21
【问题描述】:

在 mysql 中有 select * from table where 条件和(条件或条件)。 这也有codeigniter的代码吗? 示例伪代码

$this->db->select("*");
$this->db->from("table");
$this->db->where("condition", $var):
.... now the second where should be --> and (condition or condition)

【问题讨论】:

标签: php mysql codeigniter


【解决方案1】:

要通过多个条件进行查询,请将它们传递到这样的数组中:

$this->db->select('*');
$this->db->from('table');
$this->db->where(array('condition'=>$var_1, 'condition_2'=>$var_2, 'condition_3'=> $var_3)):
$result = $this->db->get()->result_array(); //returns result as array

但这取决于您的具体查询,因为某些类型的查询/条件不适用于这种格式。

编辑:这就是你要找的东西:

$this->db->select('*');
$this->db->from('table');
$this->db->where('condition_1'=>$var_1);
$this->db->where("(condition_2=1 OR condition_2=2)", NULL, FALSE);
$query = $this->db->get()->result_array();

【讨论】:

  • 我认为这就是我想要的。我要试试这个。
【解决方案2】:

希望这可行

$this->db->select("*");
$this->db->where('condition',$var);
$this->db->or_where(array('condition'=>$var,'condition'=>$var));
$qry = $this->db->get("table");
print_r($qry->result());

一切顺利。

【讨论】:

  • 你必须得到一个resultrow,如果你只是做->get 它是行不通的
  • 我希望他知道..wat 需要的是“和”和“或”的优先级...无论如何感谢您的建议..我更新得更好
  • 它应该是 $this->db->where() where in mysql where condition AND (condition or condition)
  • 它完全可以工作 lyk dat...条件和(条件或条件)...它已经过测试并且可以完美运行..
猜你喜欢
  • 2023-03-18
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2013-10-04
  • 2011-03-29
相关资源
最近更新 更多