开发者

Mysql date problem

开发者 https://www.devze.com 2023-03-02 01:49 出处:网络
i have a date field in db. I want all the data between Saturday Sunday and Monday . Of this data i want only 1 record that is record whose date is the n开发者_运维知识库ext coming date

i have a date field in db.

  1. I want all the data between Saturday Sunday and Monday .
  2. Of this data i want only 1 record that is record whose date is the n开发者_运维知识库ext coming date

Hope i make my self clear


SELECT DAYOFWEEK(date) as wd FROM table WHERE wd > 1 AND wd < 6 AND date >= CURRENT_TIMESTAMP() ORDER BY date ASC LIMIT 1;


$from = strtotime('saturday');
$to   = strtotime('tueseday') - 1; // whole monday

mysql_query('SELECT * FROM table WHERE date >= ' . $from . ' AND date <= ' . $to . ' ORDER BY date LIMIT 1');


This will return the data you need from the database:

//once you've connected to the DB
$db_query= "SELECT * FROM table 
            WHERE DATEDIFF(CURDATE(), date)>0 
            AND (DAYOFTHEWEEK(date)<3 OR DAYOFTHEWEEK(date)==7)
            ORDER BY date
            LIMIT 0,1";//limits it to one (date) result
$result=mysql_query($db_query);
if(!$result){ die('ERROR: Query failed.'); }

This will print the resultant row on screen:

while($ROW=mysql_fetch_assoc($result))
{
    echo "<p>date: {$ROW['date']}</p>";
    //do the same for all the fields you want like so:
    echo "<p>other field: {$ROW['other field']}</p>";
}

mysql_free_result($result);


SELECT * FROM table WHERE DATEDIFF(CURDATE(), date)>0 AND (DAYOFWEEK(date) in (1,2,7) ORDER BY date LIMIT 0,1

0

精彩评论

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

关注公众号