How to exclude the rows which have matching column values using Magento collections? For example, I have a table as below
coupon_name | coupon_code
C1 | C1
C2 | C2
C4 | C5
C6 | C6
From the above table I need only the rows which don't have matching column values that is C4 as it is not matching with C5. Rest of the rows are equal to the other column value. Please help me how to do this using Magento collections or at least using sql query.
Solved! Go to Solution.
I dont know the way using Magento collection but using row query its possible based on above solutions. Many times some manual query is not suitable with collection at that time you have to go with direct sql query in magento.
You can create below sql for your requirements,
select * from `tablename` where a1 != b1
Where a1 and b1 are the column name.
For Magento 1,
Use direct sql query,
<?php $resource = Mage::getSingleton('core/resource'); $readConnection = $resource->getConnection('core_read'); $query = 'select * from `tablename` where a1 != b'; $results = $readConnection->fetchAll($query); var_dump($results);
Hi, @kalyanchakri, If the answer is helpful, then accept as solutions your question so other community users helpful in future. If not working then let me know your issues.
Hi @Rakesh Jesadiya,
Thanks for the reminder.
Actually, I thought of asking you a question. Your answer helped but is there any other way to achieve it through Magento collections instead of using MySQL query?
I dont know the way using Magento collection but using row query its possible based on above solutions. Many times some manual query is not suitable with collection at that time you have to go with direct sql query in magento.