addAttributeToFilter Conditionals
There are many operators in SQL and addAttributeToFilter will accept all of them, as long as you use the correct syntax. I've listed them all below and provided examples.
Equals: eq
This is the default operator and does not need to be specified. Below you can see how to use the operator, but also how to skip it and just enter the value you're using.
$_products->addAttributeToFilter('status', array('eq' => 1)); // Using the operator
$_products->addAttributeToFilter('status', 1); // Without using the operator
Not Equals - neq
$_products->addAttributeToFilter('sku', array('neq' => 'test-product'));
Like - like
$_products->addAttributeToFilter('sku', array('like' => 'UX%'));
One thing to note about like is that you can include SQL wildcard characters such as the percent sign, which matches any characters.
Not Like - nlike
$_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));
In - in
$_products->addAttributeToFilter('id', array('in' => array(1,4,98)));
When using in, the value parameter accepts an array of values.
Not In - nin
$_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));
NULL - null
$_products->addAttributeToFilter('description', array('null' => true));
Not NULL - notnull
$_products->addAttributeToFilter('description', array('notnull' => true));
Greater Than - gt
$_products->addAttributeToFilter('id', array('gt' => 5));
Less Than - lt
$_products->addAttributeToFilter('id', array('lt' => 5));
Greater Than or Equals To- gteq
$_products->addAttributeToFilter('id', array('gteq' => 5));
Less Than or Equals To - lteq
$_products->addAttributeToFilter('id', array('lteq' => 5));
Debugging The SQL Query
There are two ways to debug the query being executed when loading a collection in Magento.
/*
* This is the better way to debug the collection
*/
$collection->load(true);
/*
* This works but any extra SQL the collection object adds before loading
* may not be included
*/
echo $collection->getSelect();
Reference Link https://fishpig.co.uk/magento/tutorials/addattributetofilter/