0

How to do that in magento model way

Select sum(qty + product_id - any_other_columns) as total from table2  where total>10;

2 Answers 2

0

Its a strange query you want to run, but this is how you do it:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', null, 'left');
$collection->getSelect()->columns(array('total' => new Zend_Db_Expr("at_qty.qty - e.entity_id")));
$collection->getSelect()->having('total > 10');
0

Here is the example how to use Direct mysql in Magento

/**
 * Get the resource model
 */
$resource = Mage::getSingleton('core/resource');

/**
 * Retrieve the write connection
 */
$writeConnection = $resource->getConnection('core_write');

/**
 * Retrieve our table name
 */
$table = $resource->getTableName('catalog/product');

/**
 * Set the product ID
 */
$productId = 44;

/**
 * Set the new SKU
 * It is assumed that you are hard coding the new SKU in
 * If the input is not dynamic, consider using the
 * Varien_Db_Select object to insert data
 */
$newSku = 'new-sku';

$query = "UPDATE {$table} SET sku = '{$sku}' WHERE entity_id = "
         . (int)$productId;

/**
 * Execute the query
 */
$writeConnection->query($query);

For More see here.

Another way

$_products = Mage::getResourceModel('catalog/product_collection')
   ->addAttributeToSelect(array('name', 'product_url', 'small_image'))
   ->addAttributeToFilter('sku', array('like' => 'UX%'))
    ->load();

For more detail see here.

2
  • you didn't read my question i think Commented May 13, 2016 at 10:29
  • I have read your question and these are just examples now what do you want ?
    – Arunendra
    Commented May 13, 2016 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.