Home  >  Article  >  Backend Development  >  怎么用PHP从MySQL数据库里面查询出日期和ID最小的一条记录呢

怎么用PHP从MySQL数据库里面查询出日期和ID最小的一条记录呢

WBOY
WBOYOriginal
2016-06-13 12:56:041007browse

如何用PHP从MySQL数据库里面查询出日期和ID最小的一条记录呢?

本帖最后由 ShunYea 于 2013-01-16 23:10:39 编辑 例如数据库里面有多条各种不同日期的记录,每个日期又会有多条,例如:

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

例如我现在想查处uid=1的日期最小的一条记录,应该是ID为2和6的记录,但是ID为2的最小,就取了这条记录,并将date为2012-12-02显示出来。

请教这个SQL语句要怎么写?谢谢。

------解决方案--------------------
$user_query4 = mysql_query("SELECT min(date) FROM table WHERE uid = '1'");<br />
$row4 = mysql_fetch_array($user_query4);<br />
// 或者你需要为 min(date) 指定别名为 date,否则是不能那么用的<br />
echo $row4['min(date)'];<br />
<br />
// 或者通过字段偏移量来代替上面的两句,效率更好<br />
mysql_result($user_query4, 0, 0);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn