Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get "not equal" in Where?

I have something like this:

    $sql = new Sql($this->tableGateway->getAdapter());
    $select = $sql->select();

    $select
        -> from('mytable')
        -> join('exp', 'field1_id=field1.id', '*', 'LEFT');

    $where = new  Where();
    $where -> equalTo('field2_id', $field2_id);

    $select->where($where);

    $statement = $sql->prepareStatementForSqlObject($select);

but what if I want use "not equalTo"? How to do it?

like image 611
Nips Avatar asked Apr 13 '13 19:04

Nips


People also ask

How can you write not equal to in the WHERE?

The SQL not equal operator is <>. You should specify this in a WHERE statement. This lets you select rows where a particular column's contents is not equal to the value you have specified. You can also use !=

Is != The same as <>?

Here is the answer – Technically there is no difference between != and <>. Both of them work the same way and there is absolutely no difference in terms of performance or result.

How do you use not equal to in query?

The SQL Not Equal comparison operator (!=) is used to compare two expressions. For example, 15 != 17 comparison operation uses SQL Not Equal operator (!=) between two expressions 15 and 17.

How do I find not equal to in SQL Server?

Not Equal To (Transact SQL) - exclamationTests whether one expression is not equal to another expression (a comparison operator). If either or both operands are NULL, NULL is returned. Functions the same as the <> (Not Equal To) comparison operator.


Video Answer


2 Answers

You need to use notEqualTo

$where->notEqualTo('field2_id', $field2_id);

http://framework.zend.com/apidoc/2.1/classes/Zend.Db.Sql.Predicate.Predicate.html#notEqualTo

like image 171
Bukashk0zzz Avatar answered Nov 04 '22 12:11

Bukashk0zzz


This is how I am using it, most of us might be searching for such querystring, having such clasues in a single query.

return $details = $this->select(function(Select $select) use ($domain, $domainWithoutWWW){
            $select->columns(['dealername'=> 'dealer.name']);
            $select->join('template',"dealer.template_id = template.id", ['template_name' => 'name','template_html' => 'html','template_css' => 'css'], 'left');
            $select->where($select->where->notEqualTo('dealer.used_car_dealer_id',1826));
            $select->where(array('dealer.parent' => 0));
            $select->where(new In('domain', ['abc.com', 'xyz.com', 'lmn.com']));
        });
like image 34
Ankit Vishwakarma Avatar answered Nov 04 '22 12:11

Ankit Vishwakarma