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?
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 !=
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.
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.
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.
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
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']));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With