如果其他行在MySQL和CodeIgniter中具有值,则禁止选择列

时间:2016-11-24 04:00:29

标签: mysql codeigniter

+------+------+
| col1 | col2 |
+------+------+
|    1 |    0 |
|    1 |    3 |
|    1 |    2 |
+------+------+

如果col1 = 1 col20,即使在其他行中,我如何阻止mysql返回col2 = 0行?所以在这种情况下,我不想获得任何行,因为第一行有col1 = 1 AND col2 = 1

修改

如果这是在第一行中col1 = 1 AND col2 = 0
而不是//case 3: two child else { int min = findRightMin(right); root.setData(min); root.setRight(remove(right, min)); return root; }

然后结果应该是三行
否则应该没有结果

2 个答案:

答案 0 :(得分:0)

 $array = array('col1' => '1','col2 !=' => '0');
    $this->db->select('col1,col2');
    $this->db->from('table');
    $this->db->where($array);
    $query = $this->db->get();

我认为这对你有用..

或者如果你想要所有行col2!= 0然后将数组更改为

$array = array('col2 !=' => '0');

或者你可以像@Shihas建议的那样简单地使用mysql方式

$where = "col1='1' AND col2!='0'";
$this->db->where($where);

希望这有帮助。

根据您的要求修改我的答案..

    $sql = mysqli_query($this->db,"SELECT col1,col2 from table where col2=0") or die(mysqli_error($db));

                    if(mysqli_num_rows($sql)>0){
                            $error = array("status"=>"Failed","msg"=>"There is data exist with col2 as 0");
                            $this->response($this->json($error),400);
                    }
                    else{
//put your condition here
                         $sql = mysqli_query($this->db,"SELECT col1,col2 from table where col1=1 col2!=0") or die(mysqli_error($db));

                    }

你可以把你的查询放在其他部分,无论你想要执行什么

答案 1 :(得分:0)

最后,我发现了自己问题的答案。诀窍是使用group byhaving子句

SELECT col1 FROM table WHERE col1 = 1 GROUP BY col2 HAVING col2 != 0

非常感谢那些试图回答的人。