View Full Version : Distinct & Limit Not Working

11-11-2011, 05:08 AM
Hi guys,

DB is mysql.
I have the below query which i'm trying to run through PHP.

$sql = "SELECT DISTINCT pid FROM campaign_details WHERE camp_id='$camp_id' LIMIT 7";
$newquery = mysql_query($sql) or die(mysql_error());

There are 10 distinct pid values but I want only 7.
The stupid thing returns all of them when I need to limit them at 7.

Any work around this?
Or Do I need to go and take a coffee break and have another look? haha.


Old Pedant
11-11-2011, 07:40 PM
Ummm...so just limit it to 7 in the PHP code.

$cnt = 7;
while ( ...fetch one row ... )
if ( --$cnt == 0 ) break;

Old Pedant
11-11-2011, 07:42 PM
But now that I look at your query again, I don't see why it isn't working.

Try running the query *OUTSIDE* of PHP and see what you get.