Jump to content

finding the last record in array from mysql


giraffemedia

Recommended Posts

Hello,

 

I have a table in my database that contains a list of issuse_numbers, each with a corresponding booking form number next to it like below…

id(primary)     issue_number        booking_form_number

12306	92	3089
12305	93	3088
12304	92	3088
12303	94	3087
12302	93	3087
12301	92	3087
12300	112	3085
12299	111	3085
12298	110	3085
12297	109	3085
12296	108	3085
12295	107	3085
12294	106	3085
12293	105	3085
12292	104	3085
12291	103	3085
12405	103	3080
12404	102	3080
12403	101	3080
12402	100	3080
12401	99	3080
12285	98	3079
12284	97	3079
12283	96	3079
12282	95	3079
12281	94	3079
12280	93	3079
12279	92	3079
12278	91	3079
12365	92	3078
12364	91	3078
12363	90	3078

 

I would like to be able to find out the best way of getting the last issue number for any booking form. Is this a mysql query related thing in addition to using the php end function? I'm not sure how to group the rows together so I get something like…

 

91, 92, 93, 94, 95, 96, 97, 98 for booking number 3079

90, 91, 92 for booking number 3078 etc etc

 

I would appreciate any help as it's driving me nuts!

 

James

 

Link to comment
Share on other sites

if you are looking for the maximum issue_number for a given booking_form_number

 

SELECT MAX(issue_number) FROM table_name WHERE booking_form_number = 3079

 

To add to that, if you want the max booking number for all forms then this should do the trick

SELECT booking_form_number, MAX(issue_number) FROM table_name GROUP BY booking_form_number

Link to comment
Share on other sites

if you are looking for the maximum issue_number for a given booking_form_number

 

SELECT MAX(issue_number) FROM table_name WHERE booking_form_number = 3079

 

To add to that, if you want the max booking number for all forms then this should do the trick

SELECT booking_form_number, MAX(issue_number) FROM table_name GROUP BY booking_form_number

 

That worked great thanks. I had to tweak it a bit to get it to work how I needed but the MAX() bit was the key. I ended up with the following…

 

$issue_numbers = mysql_query("SELECT ib_booking_form_number,ib_issue_number, MAX(ib_issue_number) AS end_issue FROM issues_booked GROUP BY ib_booking_form_number") or die(mysql_error());

while($row = mysql_fetch_array($issue_numbers)){
	if ($row['end_issue'] == $_GET['issue_number']) {
		$booking_number[] = $row['ib_booking_form_number'];
	}
}

 

Thanks very much everyone.

 

James

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.