How can I use PHP to query a record with the minimum date and ID from the MySQL database?

Source: Internet
Author: User
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.