Page 1 of 1

Get youngest ID after getting all rows[topic solved]

Posted: Tue Oct 23, 2007 12:22 pm
by SirChick
I have this query:

Code: Select all

$GetBusinessType = mysql_query("SELECT * FROM businesstype
                    WHERE Businesstypes='$Business'")
				or die(mysql_error());	
                  
// Fetch the row from the database
if (!($row = mysql_fetch_assoc($GetBusinessType))) {
    echo "Business not found!";
    echo mysql_error();
}

And it works dandy but I want to change it so that it does something like this.. say theres 3 rows in the database:

Type - ID
Shop - 20
Shop - 13
Shop - 14


What i want my query to do is find the business type which is does.. and get all the rows and then select only the lowest ID out of the 3.
So it would load up Shop - 13 in other words... is this possible?

Posted: Tue Oct 23, 2007 12:35 pm
by feyd
I would suspect GROUP BY and ORDER BY clauses would probably do what you wish.

Posted: Tue Oct 23, 2007 12:43 pm
by SirChick
Would it really need group by? As its only going to find the one type of shop anyway [s]cos[/s] because of this Businesstypes='$Business'

say $Business was = Shop... then group by won't be needed.. surely?
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.

Limit statement

Posted: Tue Oct 23, 2007 12:59 pm
by churt
Order by the id field and use the Limit statement

Code: Select all

$GetBusinessType = mysql_query("SELECT * FROM businesstype
                    WHERE Businesstypes='$Business' order by id-field Limit 0,1")
                                or die(mysql_error());

Posted: Tue Oct 23, 2007 1:20 pm
by SirChick
Thankyou :)