cancel
Showing results for 
Search instead for 
Did you mean: 

How to exclude the rows which have matching column values using Magento collections

SOLVED

How to exclude the rows which have matching column values using Magento collections

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.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to exclude the rows which have matching column values using Magento collections

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.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

4 REPLIES 4

Re: How to exclude the rows which have matching column values using Magento collections

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);
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: How to exclude the rows which have matching column values using Magento collections

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.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: How to exclude the rows which have matching column values using Magento collections

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?


Re: How to exclude the rows which have matching column values using Magento collections

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.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial