Get youngest ID after getting all rows[topic solved]

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Get youngest ID after getting all rows[topic solved]

Post 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?
Last edited by SirChick on Tue Oct 23, 2007 1:21 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would suspect GROUP BY and ORDER BY clauses would probably do what you wish.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post 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.
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Limit statement

Post 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());
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

Thankyou :)
Post Reply