Quite often when working with collections in Magento need to filter collection by product attributes, here comes addAttributeToFilter method. This method works on EAV collections in Magento. For example it could be product collection, customer collections and category collections. The main aim of this method is to set WHERE condition in SQL query. Lest look at example listed below.

$_products = Mage::getResourceModel('catalog/product')->getCollection()
    ->addAttributeToFilter('sku', array('like' => 'FGT%'))

The provided code would try to get a product collection, with filters SKUs starting with FGT. The LIKE word here is the very conditional operand.

Attribute filter Conditionals

There are many other conditionals in SQL except LIKE. We can put it in addAttributeToFilter method.

Like - like

$_products->addAttributeToFilter('sku', array('like' => 'FGT%'));

Not Like - nlike

$_products->addAttributeToFilter('sku', array('nlike' => 'SOME-SKU%'));

Equals: eq

$_products->addAttributeToFilter('sku', array('eq' => 'SOME-SKU'));
$_products->addAttributeToFilter('sku', 'SOME-SKU');

Not Equals - neq

$_products->addAttributeToFilter('sku', array('neq' => 'SOME-SKU'));

In - in

$_products->addAttributeToFilter('id', array('in' => array(1,2,3,4)));

Not In - nin

$_products->addAttributeToFilter('id', array('nin' => array(1,2,3,4)));

NULL - null

$_products->addAttributeToFilter('name', array('null' => true));

Not NULL - notnull

$_products->addAttributeToFilter('name', array('notnull' => true));

Greater Than - gt

$_products->addAttributeToFilter('id', array('gt' => 10));

Less Than - lt

$_products->addAttributeToFilter('id', array('lt' => 11));

Greater Than or Equals To - gteq

$_products->addAttributeToFilter('id', array('gteq' => 12));

Less Than or Equals To - lteq

$_products->addAttributeToFilter('id', array('lteq' => 14));

To use this conditionals in not EAV models you just need to change function from addAttributeToFilter to addFieldToFilter.