In a MySQL table, I would like to take 10 records with DISTINCT values.
I am using Zend Framework.
$select = $this->getAdapter()->select()
->from('table', 'column')->group('column')
->limit(10, 0);
This is the query generated by the above code.
SELECT table.column FROM
table GROUP BY column LIMIT 10
What happens here is that MySQL is taking 10 records first and then applying the group by. So finally, I am getting only 7 records.
How to apply DISTINCT first and then take 10 records from it?
Test that SQL against a table — MySQL applies the
limitlast, so doesn’t do what you’re saying. eg test againstand do
select A.a1 from A group by a1 limit 2. You should see 1, 2, not 1, 1.[I wanted to say this as a ‘comment’ rather than an ‘answer’, but couldn’t]