In magento a model is required to perform SQL query in your database tables.
Sort data:
$collection = Mage::getModel('module/model_name')->getCollection() ->addAttributeToSort('order', 'ASC') ->addAttributeToSort('last_name', 'ASC') ->addAttributeToSort('first_name', 'ASC');
Or Condition:
// Add OR condition: $collection->addAttributeToFilter(array( array( 'attribute' => 'field_name', 'in' => array(1, 2, 3), ), array( 'attribute' => 'date_field', 'from' => '2000-09-10', ), ));
$filter_a = array('like'=>'a%'); $filter_b = array('like'=>'b%'); Mage::getModel('catalog/product') ->getCollection() ->addFieldToFilter('sku', array($filter_a, $filter_b)) ->getSelect();
Equal:
$collection->addAttributeToFilter('field_name', 1);
Where:
$collection->addAttributeToFilter('field_name', array( 'in' => array(1, 2, 3), )); $collection->addAttributeToFilter('date_field', array( 'from' => '2000-09-10', )); $collection->addAttributeToFilter('date_field', array( 'from' => '10 September 2000', 'to' => '11 September 2000', 'date' => true, // specifies conversion of comparison values )); $collection->addAttributeToFilter('field_name', array( 'notnull' => true, ));
Update data:
$collection->setColumnname('value'); $collection->save();
Many more here:
https://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento
and here
https://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections
Want direct SQL query ???
/** * Get the resource model */ $resource = Mage::getSingleton('core/resource'); /** * Retrieve the read connection */ $readConnection = $resource->getConnection('core_read'); $query = 'SELECT * FROM ' . $resource->getTableName('catalog/product'); /** * Execute the query and store the results in $results */ $results = $readConnection->fetchAll($query); /** * Print out the results */ var_dump($results);