How to use PHP from the MySQL database to query out the date and the lowest ID of a record?

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