Codeigniter mysql where不等于查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14640014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter mysql where not equal to query
提问by Avinash
Mysql codeigniter query is not working properly. Suppose if mysql table looks like this:
Mysql codeigniter 查询无法正常工作。假设 mysql 表如下所示:
user_id|user_name
1|john
2|alex
3|sam
Here user_nameis unique
这里user_name是唯一的
The following query should return falseif user_name=johnand user_id=1and trueif say user_name=johnand user_id=2.
下面的查询将返回错误的,如果USER_NAME =约翰和USER_ID = 1和真如果说USER_NAME =约翰和USER_ID = 2。
$this->db->get_where('user', array('user_name' => $name,'user_id !=' => $userid));
But it returns truein the case user_name=johnand user_id=1.
但在user_name=john和user_id=1的情况下它返回true。
Can anyone suggest me an alternative way of querying not equal to.
谁能建议我另一种查询方式不等于。
print($this->db->last_query()) gives:
print($this->db->last_query()) 给出:
SELECT * FROM (user) WHERE user_name = 'john' AND user_id != '1'
SELECT * FROM (user) WHERE user_name = 'john' AND user_id !='1'
采纳答案by rohitarora
Why dont you use simple $this->db->query('your query');
你为什么不使用简单的 $this->db->query('your query');
回答by Ameen Maheen
Simply try this
试试这个
$this -> db -> where('invitee_phone !=', $user_phone);
回答by Sachin
You can go follwoing way too. It work for me
您也可以按照以下方式进行。它对我有用
$total = 5;
$CI = get_instance();
$CI->load->database();
$CI->db->order_by('id','asc');
$topusers = $CI->db->get_where('users',array('user_type != 1 && `status` =' => 1),$total,0);
echo $CI ->db ->last_query();
die;
and if still not work for you can go with @rohit suggest: $this->db->query('your query');
如果仍然不起作用,您可以使用@rohit 建议: $this->db->query('your query');
回答by Aravindh Gopi
Type 1:
类型 1:
Using ->where("column_name !=",$columnname)
is fine for one column.
->where("column_name !=",$columnname)
对一列使用很好。
But if you want to check multi columns, you have to form an arrayinside whereclause.
但是如果要检查多列,则必须在where子句中形成一个数组。
Like this
像这样
$whereArray = array(
"employee_name" => $name,
"employee_id !=" => $id,
);
$this->db->select('*')->from('employee')->where($whereArray);
Type 2:
类型 2:
We can just write exactly what we want inside where.
我们可以在 where 里面准确地写出我们想要的东西。
Like
喜欢
$thi->db->where(("employee_id =1 AND employee name != 'Gopi') OR designation_name='leader@gopis clan'");
Type 2is good for working with combining queries, i mean paranthesis"()"
类型 2适用于组合查询,我的意思是括号“()”
回答by Gaurav
you can follow this code:
你可以按照这个代码:
$query = $this->db->select('*')->from('employee')->where('user_name', $name)->where('user_id !=', $userid)->get();
$last_query = $this->db->last_query();
$result = $query->result_array();
if you pass $name = 'john' and $userid = '1' then it return empty array.
如果你传递 $name = 'john' 和 $userid = '1' 那么它返回空数组。