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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.