Mysql查询:检索当前日期查询 [英] Mysql query: retrieve current date query

查看:105
本文介绍了Mysql查询:检索当前日期查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mysql数据库中,我有这个列:



名称:日期



p>

我在该列中有几个值:



2009-01-05 01:23:35



2009-03-08 11:58:11



2009-07-06 10:09:03



如何检索当前日期?我在php中使用php。



 <? php $ today = date('Ym-d');?> 

如何编写一个mysql查询以检索今天的所有日期数据?



如果我将列类型更改为date,那么只插入值2009-07-06,没有时间值

解决方案

你不需要使用PHP,MySQL有一个函数来获取当前日期:

 code> SELECT field FROM table WHERE DATE(column)= CURDATE()



< a href =http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_curdate =noreferrer> CURDATE , DATE



如果您的列只需要日期部分,而不是时间,您应该将列类型更改为DATE。如果你坚持通过PHP做它,它是一样的东西,真的:

  $ today = date('Ym-d '); 
$ query = mysql_query(
SELECT field FROM table WHERE DATE(column)='$ today'
);


In mysql database i have this column called:

Name: Date

Type: datetime

I have few values in that column:

2009-01-05 01:23:35

2009-03-08 11:58:11

2009-07-06 10:09:03

How do I retrieve current date? I am using php.

in php:

<?php $today = date('Y-m-d');?>

How to write a mysql query to retrieve all today date data?

Should i change the column type to "date", then insert values like "2009-07-06" only, no time values???

解决方案

You don't need to use PHP, MySQL has a function to get the current date:

SELECT field FROM table WHERE DATE(column) = CURDATE()

Documentation: CURDATE, DATE.

If your column is only ever going to need the date part and never the time, you should change your column type to DATE. If you insist on doing it through PHP, it is the same thing, really:

$today = date('Y-m-d');
$query = mysql_query("
    SELECT field FROM table WHERE DATE(column) = '$today'
");

这篇关于Mysql查询:检索当前日期查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆