How can I use PHP to query a record with the minimum date and ID from the MySQL database? This post was last edited by ShunYea from 2013-01-1623: 10: 39. for example, the database contains multiple records of different dates, and each date contains multiple records. for example: how can I use PHP to query a record with the minimum date and ID from the MySQL database?
This post was last edited by ShunYea at 23:10:39. for example, the database contains multiple records of different dates, and each date contains multiple records. 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, if I want to investigate a record with the minimum date of uid = 1, it should be a record with IDs of 2 and 6, but the record with the minimum ID of 2 takes this record, and display the date as 2012-12-02.
How can I write this SQL statement? Thank you.
------ Solution --------------------
$ User_query4 = mysql_query ("SELECT min (date) FROM table WHERE uid = '1 '");
$ Row4 = mysql_fetch_array ($ user_query4 );
// Or you need to specify the alias for min (date) as date. Otherwise, the Alias cannot be used.
Echo $ row4 ['min (date) '];
// Or use the field offset to replace the above two sentences, which is more efficient.
Mysql_result ($ user_query4, 0, 0 );