Jump to content

MYSQL Limit by id?


Mortenjan

Recommended Posts

Hi. I am trying to limit rows in mysql based on id.

Im using a left join.

 

If i have this setup:

id

175

175

185

196

198

 

And i want mysql to limit to the first 2 id numbers so the output becomes this:

 

175

175

185

 

How can i achieve this?

 

my mysql query looks like this:

SELECT
c2.blog_id,
c2.blog_title_en,
c2.blog_title_no,
c2.blog_image,
c2.blog_content_en,
c2.blog_content_no,
c2.blog_date,
c2.mms,

c1.id,
c1.mmsid,
c1.datalen,
c1.content_type,
c1.data

FROM parts AS c1
RIGHT JOIN blog AS c2

ON c1.mmsid = c2.blog_id
WHERE `content_type` != 'application/smil' OR `content_type` IS NULL

ORDER BY  c2.blog_date DESC, c1.id ASC

 

Thanks in advance:)

Regards Morten

Link to comment
https://forums.phpfreaks.com/topic/209922-mysql-limit-by-id/
Share on other sites

I use this command so it wont display the same id twise:

 

if ($lastblog != $blog_array['blog_id']) {

// CODE

$lastblog = $blog_array['blog_id'];
}

 

But i still have an issue.

If i have this result from my query:

 

id:

125

125

196

196

 

It only displays this far:

125

125

196

 

How can i get it to display the last row?

 

Thank you so far :)

 

Regards Morten

 

Link to comment
https://forums.phpfreaks.com/topic/209922-mysql-limit-by-id/#findComment-1095716
Share on other sites

You need a variable to indicate when the number of unique iterations have been meant.

$keepDisplayingRows = true;

 

Set this to false when a new id is displayed. But keep looping through the result set to display the remaining matching ids.

 

The logic is pretty simple. Post your loop code if it is not working.

Link to comment
https://forums.phpfreaks.com/topic/209922-mysql-limit-by-id/#findComment-1095759
Share on other sites

$keepDisplayingRows = true;
while ($blog_array = mysql_fetch_assoc($blog_result)){ 
if($keepDisplayingRows == true){
	if($lastblog != $blog_array['blog_id']){
	$lastblog = $blog_array['blog_id'];
	$i++;	
	if($i == $limit){
		$keepDisplayingRows = false;
		}	
	}
           // REST OF THE CODE GOES HERE
           }
}

 

Hm, i understand what you mean, but im not sure how keep looping through the rest of the ids. Do you think you could help me?

That would be much appreciated.

 

Regards Morten

Link to comment
https://forums.phpfreaks.com/topic/209922-mysql-limit-by-id/#findComment-1095781
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.