TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   MySQL & Databases (http://www.talkphp.com/mysql-databases/)
-   -   Problem with second query ... (http://www.talkphp.com/mysql-databases/6037-problem-second-query.html)

pepelepew1962 11-16-2011 08:41 PM

Problem with second query ...
 
Hello:

I am having a problem and I am not sure my method is correct. I know my sub-query isn't. What I need is to get the Max-Autonum, or last record for a specific record. Please see the example for a better understanding:



part_id_num part_manufac_code part_number
1 36 18547
2 36 57847
3 35 79811
4 36 57914
5 35 74992


If I am looking for part_manufac_code 36, then the last record (part_id_num) for that is 4. Here is the code that I have:
//
//
//
require("connect.php");
//
//
//
$query1 = "SELECT part_id_num, part_manufac_code, part_number FROM tblparts WHERE (part_manufac_code = '$frmfindpart')";
$result1 = mysql_query($query1) or die(mysql_error());
//
//
$query2 = "SELECT MAX(part_id_num) FROM '$result1'";
$result2 = mysql_query($query2) or die(mysql_error());
if(mysql_num_rows($result2) > 0)
{
while($row = mysql_fetch_assoc($result2))
{
$names[] = $row['name'];
}
}
else
{
echo 'query returned no results';
}

RokD 11-16-2011 10:03 PM

This should probs do the trick
PHP Code:

$query "SELECT * FROM tblparts ORDER BY part_manufac_code DESC, part_id_num DESC LIMIT 1"

$row mysql_fetch_arraymysql_query$query ));

echo 
"The result is {$row['part_number']}"


tony 11-17-2011 03:03 PM

You beat me to it RokD :P. But yeah, a combination or ORDER BY and LIMIT would do the trick.


All times are GMT. The time now is 05:06 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0