0

我想从varchar存储的日期(例如12-21-1990)中获取日期。我可以使用 phpexplode获取当前日期,但我想通过 mysql 获取它。我正在使用这样的查询:

$count=mysql_query("SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id "); 

上面的查询为我获取了所有详细信息的用户朋友,但我只想获取今天生日的朋友详细信息。我怎样才能做到这一点?

4

6 回答 6

2

您可以使用 php date function 获取今天的日期,将其分配给变量并检查 u.bday 。像这样:

$count=$today = date("m-d-Y");

mysql_query("SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id AND u.bday = $today");
于 2012-08-21T12:04:21.660 回答
0

很简单,您只需要使用 STR_TO_DATE MySQL 函数将您的 varchar 转换为日期:

$count=mysql_query("SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id AND STR_TO_DATE(u.bday, '%m-%d-%Y')=DATE_FORMAT(CURDATE().'%m-%d-%Y') "); 

但我强烈建议您将 bday 列更改为日期类型。

于 2012-08-21T12:17:00.253 回答
0

尝试这个 :

date_format(your_friend_birthdate,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')

完整查询:-

    SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM 
friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id 
and month(u.bday)=month(now()) and date(u.bday)=date(now());

或者

SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM 
    friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id 
    and month(u.bday)=month(now()) and day(u.bday)=day(now());
于 2012-08-21T12:19:26.660 回答
0

你好刚刚找到解决方案并想分享它,以便它可以帮助某人。

SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM 
    friend_list f, users_profile u WHERE f.uid = '3' AND f.status = '1' AND u.uid = f.friend_id 
      and    DAY(STR_TO_DATE(u.bday, '%m-%d-%y')) = DAY(CURDATE()) AND
    MONTH(STR_TO_DATE(u.bday, '%m-%d-%y')) = MONTH(CURDATE())
于 2012-08-21T19:59:03.203 回答
0

我会将 bday 列数据类型更改为DATE并使用CUREDATE()函数

于 2012-08-21T12:07:28.803 回答
0
SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status FROM 
friend_list f, users_profile u WHERE f.uid = '$id' AND f.status = '1' AND u.uid = f.friend_id 
and CAST(CONCAT(SUBSTRING(u.bday,7,11),'/',SUBSTRING( u.bday,1,2 ),'/',SUBSTRING(u.bday,4,2 )) AS DATE) = date_format(now(),'%Y-%m-%d');
于 2012-08-21T12:29:31.303 回答