开发者

MySql Query to display a record with time condition

For example i am posting a news via admin and mentioning the start date as 25-02-2011 and end date as 28-02-2011. It will display in the user side on 25the feb 2011.

If i post a news via admin on start date as 26-02-2011 and end date as 28-02-2011 then it will display the news on 26-02-2011.

If i didn't post any开发者_开发技巧thing on 27-02-2011 and 28-02-2011 it should display the last posted news. That is in this case it should display the news which is posted on 26-02-2011 for both 27-02-2011 and 28-02-2011.

EDIT: "SELECT id, title FROM news WHERE startDate >= CURDATE() AND

expiryDate <= CURDATE() AND status='Active' ORDER BY id DESC". There is a news in current date. It is not even fetching that record. What change to be done.

How can this be done using mysql.

thanks in advance...


If this is a website you are talking about, please mention the server side technology you are using. For example is it using php? Because what you are asking for, can not be accomplished just by using mysql.


select * from news where startdate >= now() and enddate <= now() limit 1

You'll have to change table and columns name.


It looks like you've just mixed up the operators. Today should be after the startdate, doens't it?

SELECT id, title FROM news WHERE startDate <= CURDATE() AND
expiryDate >= CURDATE() AND status='Active' ORDER BY id DESC


Try this code:

SELECT id, title FROM news WHERE now() between startDate AND expiryDate AND status='Active' ORDER BY id DESC

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜