How do I use PHP to query the MySQL database for a record with the smallest date and ID?
This post was last edited by Shunyea on 2013-01-16 23:10:39
For example, there are multiple records of different dates in the database, and there will be multiple entries for each date, for example:
ID Date UID
2 2012-12-02 1
3 2012-12-02 2
4 2012-12-03 2
5 2012-12-03 1
6 2012-12-02 1
7 2012-12-04 2
8 2012-12-03 1
For example, I now want to investigate the uid=1 date of the smallest record, should be the ID 2 and 6 of the record, but the ID of 2 is the smallest, take this record, and the date for 2012-12-02 display.
How do you write this SQL statement? Thank you.
------Solution--------------------
$user _query4 = mysql_query ("Select min (date) from table WHERE uid = ' 1 '");
$row 4 = mysql_fetch_array ($user _query4);
Or you need to specify the alias date for min (date), otherwise it won't work.
echo $row 4[' min (date) ';
or the field offset to replace the above two sentences, more efficient
Mysql_result ($user _query4, 0, 0);