我正在查询接下来10天的生日。我今天能够查询,但即使在推荐一些帖子后也未能在接下来的10天内完成。以下是我今天过生日的问题
$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 DAY(STR_TO_DATE(u.bday, '%m-%d-%y')) = DAY(CURDATE())
AND MONTH(STR_TO_DATE(u.bday, '%m-%d-%y')) = MONTH(CURDATE())");
任何人都可以帮我解决10天的查询
答案 0 :(得分:2)
如果u.bday是约会,你可以这样做:
WHERE u.bday < NOW() + INTERVAL 10 DAY AND u.bday >= NOW()
MySQL具有良好的日期比较和计算功能。有关详细信息,请参阅手册:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
如果u.bday是varchar格式的mm-dd-yyyy,请使用:
WHERE STR_TO_DATE(u.bday, '%m-%d-%Y') < NOW() + INTERVAL 10 DAY
AND STR_TO_DATE(u.bday, '%m-%d-%Y') >= NOW()
(如果u.bday不是日期,请将其设为一个。为列使用正确的数据类型可以让您的生活更轻松。)
答案 1 :(得分:0)
答案 2 :(得分: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'和f.status ='1'和u.uid = f.friend_id和DAY(STR_TO_DATE(u.bday,'%m-%d-%y'))&lt; DAY(CURDATE())+ 7和 DAY(STR_TO_DATE(u.bday,'%m-%d-%y'))!= DAY(CURDATE())AND 月(STR_TO_DATE(u.bday,'%m-%d-%y'))=月(CURDATE())“
答案 3 :(得分:0)
良好做法:始终使用与数据匹配的正确字段类型,并避免使用unix_timestamp作为日期,除非您有充分的理由。同时指定您的联接。
如果您的bday不是时间戳或日期字段(应该使用日期类型超过时间戳bdays不要更改) 然后把它写成:
SELECT u.fname, u.lname, u.profile_pic, u.uid, u.bday, f.uid, f.friend_id, f.status<br>
FROM friend_list f <br>
join users_profile u on(<br>
u.uid = f.friend_id AND f.status = '1' AND STR_TO_DATE(u.bday, '%Y-%m-%d') between date(now()) AND date(DATE_ADD(NOW(), INTERVAL 10 DAY))<br>
)<br>
where f.uid = '$id'
And if you did use a date type for the field you can change <br>
STR_TO_DATE(u.bday, '%Y-%m-%d')<br>
to<br>
date(u.bday)
答案 4 :(得分:0)
SELECT * FROM tablename WHERE DAYOFYEAR(curdate()) <= DAYOFYEAR(fieldname) AND DAYOFYEAR(curdate()) +10 >= DAYOFYEAR(fieldname) LIMIT 30;