RE: [PHP] help in getting results from mysql

From: Date: Tue, 03 Jul 2001 02:02:13 +0000
Subject: RE: [PHP] help in getting results from mysql
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-55921@lists.php.net to get a copy of this message
If you aren't concened about multiple entries with the same date, you could try: $rs1 = mysql_query("select * from tablename where item_id=34 and item_type_id=3 order by date desc limit 1"); $last_date=mysql_result($rs1,0,0); Although I don't know how efficient of an SQL query that is... Perhaps someone more qualified could comment... If you try this method, and have a large number of entries, you may want to create an index across item_id, item_type_id, and date... Hope this helps. -----Original Message----- From: David Robley [mailto:huntsman@www.nisu.flinders.edu.au] Sent: Monday, July 02, 2001 9:41 PM To: Christian Dechery; php-general@lists.php.net Subject: Re: [PHP] help in getting results from mysql On Tue, 3 Jul 2001 09:54, Christian Dechery wrote: > I have to get some results from a mysql table, and I'm pretty sure > that I'm not doing it the best way. > > its like this... I have a table (a historical one) with these fields: > date | item_id | item_type_id | qty | value > > and there are multiple lines for the pair item/type (as I said it's a > historical table). I want the row with the last date. So if I had: > > 2001-29-6 | 34 | 3 | 5 | 15.0 > 2001-30-6 | 34 | 3 | 7 | 13.0 > 2001-1-7 | 34 | 3 | 9 | 12.0 > 2001-2-7 | 34 | 3 | 1 | 3.5 > > I only want the last one '2001-2-7'. But I can't think of a way of > doing this without sub-select. So I did this: > > <pre> > $rs1=mysql_query("select max(date) from tablename where item_id=34 and > item_type_id=3"); $last_date=mysql_result($rs1,0,0); > > $rs2=mysql_query("select qty,value from tablename where > date='$last_date'"); list($qty,$value) = mysql_fetch_row($rs2); </pre> > > it works fine... but I don't think it is good... whenever there is > 'code' between queries to get results I think something could be > optimized. Any ideas?

« previous php.general (#55921) next »