Get yesterday query from MySQL - mysql

I'm trying to get queries just for yesterday! With this code, I can get queries for today, but I don't know what should i do to show just yesterday queries:
$t_date = date( 'Y-m-d H:i', $_TIME - (3596 * 24) );
$twall = $db->super_query("SELECT COUNT(*) as c
FROM dle_photo_post
WHERE date > = '$t_date'
AND date < = '$t_date' + INTERVAL 24 HOUR
AND moder = '0' ");
$twall = $twall['c'];

Try this:
SELECT COUNT(*) as c
FROM dle_photo_post
WHERE date between
date_sub(curdate(), interval 2 day)
and date_sub(curdate(), interval 1 day)
AND moder = '0'

Related

MySQL Query Comparing DateTime not give the right row

dosen_schedule_datetimestart , dosen_schedule_datetimeend , academic_schedule_datetimestart , academic_schedule_datetimeendis formatted in DATETIME Format YYYY-MM-DD HH:MM:SS
This was my MySQL Query, so what i want to do is SELECT Row that Today is still in between datetimestart and datetimeend And TIME of datetimestart is less than 15 minutes compared to current time
But it doesnt work, and i also comparing academic_schedule_dow with Today's DOW
Any Idea?
$checkdosenschedule = mysqli_query($conn, "SELECT dosen_schedule_table.*, dosen_table.*, TIME(dosen_schedule_table.dosen_schedule_datetimestart) AS dosen_schedule_checkstart FROM dosen_schedule_table
INNER JOIN dosen_table
ON dosen_table.dosen_id = dosen_schedule_table.dosen_id
WHERE (dosen_schedule_status = 'Active' AND dosen_subs_id != '' ) AND ((DATE(dosen_schedule_datetimestart) >= CURDATE() AND DATE(dosen_schedule_datetimeend) >= CURDATE()) AND (TIME(dosen_schedule_datetimestart) > (NOW() - INTERVAL 15 MINUTE)))");
$checkacademicschedule = mysqli_query($conn, "SELECT academic_schedule_table.*, dosen_table.* FROM academic_schedule_table
INNER JOIN dosen_table
ON dosen_table.dosen_id = academic_schedule_table.dosen_id
WHERE ((academic_schedule_status = 'Active' AND dosen_subs_id != '' ) AND academic_schedule_dow = DAYOFWEEK(NOW())) AND ((DATE(academic_schedule_datetimestart) >= CURDATE() AND DATE(academic_schedule_datetimeend) >= CURDATE()) AND (TIME(academic_schedule_datetimestart) > (NOW() - INTERVAL 15 MINUTE)))");
Try using DATE(NOW()) instead of CURDATE()
Also you are checking the range with same sign of greater then equal to.
If you want to check in between, use >= for start and <= for end.

How to select values from 2 hours ago from now

I am not having much knowledge on mysql queries.. and i'm trying to get values from database from last two hours from now ..I have searched about it and i have found some related posts too.. but unfortunately i am unable to implement the logic.. please help me solve this issue..
here is the mysql query
select *
from `ordermaster`
where ((
`ordermaster`.`Pick_date`
= curdate())
and (`ordermaster`.`Pick_time`
<= (now() - interval 2 hour))
and (`ordermaster`.`Status`
= 2))
were, Pick_Date = "2017-04-19" (today date) and Pick_Time = "10:00:00" (24 hours format)
Try the Following:
Select * From ordermaster om
Where Concat(om.Pick_date,' ', om.Pick_time) as date Between
(now() - interval 2 hour) and now()
AND Status = 2
You can use the between keyword
select *
from `ordermaster`
where `Pick_date` = curdate() and
`Pick_time` between (now() - interval 2 hour) and now() and
`Status` = 2
Edit
Based on our chat, where we found out the GoDaddy server you have your db hosted on is 12.5 hours ahead of your local time, the final query you should use is
select *
from `ordermaster`
where `Pick_date` = curdate() and
`Pick_time` <= now() + interval 10 hour + interval 30 minute and
`Status` = 2

sql take all querys from last month

I followed the first answer on this question but i have some problems with my sql query. I don´t get any result.
What error i made ?
How can i set CURDATE always on the first day of the month ?
SELECT DATE_FORMAT(`date`, '%Y-%m-%d') , `price`
FROM `sales`
WHERE `id` = :id
AND (`date` BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE())
Edit Whole code:
$verkaufmonat3 = 0;
$verkaufmonatanzahl2 = 0;
$verkaufmonat4 = array();
$sqluser5 = $X['dbh']->prepare("SELECT DATE_FORMAT(`date`, '%Y-%m-%d') , `preis` FROM `verkauf` WHERE `vertreterid` = :id AND (`date` BETWEEN CURDATE(), '%Y-%m-01' - INTERVAL 60 DAY AND CURDATE(), '%Y-%m-01')");
$sqluser5->execute(array(
':id'=>$_SESSION['id']
));
$verkaufmonatanzahl2 = $sqluser5->rowCount();
$verkaufmonat4 = $sqluser5->fetchAll();
for ($a = 0; $a <= $verkaufmonatanzahl2; $a++) {
$verkaufmonat3 += $verkaufmonat2[$a]['preis'];
}
In my database i have a sale on the 28.10.2016 wich has for ex. a value of 50 eur. So my $verkaufmonat3 should be 50 but it isn´t, it´s 0.
Your question isnt clear but to get the first day of current month then you use
SELECT DATE_FORMAT(CURDATE() , '%Y-%m-1')
So if you want the previous month data try
SELECT
DATE_FORMAT(CURDATE() , '%Y-%m-1') - INTERVAL 1 MONTH as first_day,
DATE_FORMAT(CURDATE() , '%Y-%m-1') - INTERVAL 1 DAY as last_day
MEANING
WHERE `date` BETWEEN DATE_FORMAT(CURDATE() , '%Y-%m-1') - INTERVAL 1 MONTH
AND DATE_FORMAT(CURDATE() , '%Y-%m-1') - INTERVAL 1 DAY

MYSQL DATE RANGE and TIME RANGE

Counting fax completed by Graveyard Shift 10pm - 7am,
date and time are in different field
$stmt = $db -> prepare("SELECT count(*) FROM fax WHERE
date BETWEEN CURDATE() and CURDATE() + INTERVAL 1 DAY
and time >= '22:00:00' and time <= '7:00:00'
and shift='GY'
and complete=1");
$stmt -> execute();
echo $GY_COMP = $stmt -> fetchColumn();
This query always resulting to 0 but it has data.
Can someone help me with my query?
Thanks in advance :)
I think the problem was in your time comparison: you effectively filtered out everything with your time >= '22:00:00' and time <= '7:00:00'
Hope this helps: http://sqlfiddle.com/#!2/45108/7/0
SELECT * FROM fax
WHERE date BETWEEN CURDATE() and CURDATE() + INTERVAL 1 DAY
and
((time >= '22:00' and time <= '23:59')
or
(time >= '0:00' and time <= '7:00'))
and shift='GY'
and complete=1
Thanks Max for the time comparison,
I already made changes to my syntax
SELECT count(*) FROM fax
WHERE ((date = CURDATE() and (time >='22:00' and time <= '23:59'))
or (date=CURDATE() + INTERVAL 1 DAY and (time >= '00:00' and time <= '7:00')))
and shift = 'GY'
and complete = 1
now my problem is RANGE of shift is today 10pm to tom 7am.
But my syntax above still get wrong when it change date
HELP IN USING IF STATEMENT in MYSQL
IF YES(SELECT THIS) ELSE IF (SELECT THIS)
HOW to properly use IF ELSE in MYSQL statement
SELECT count(*) FROM fax
IF
(date = CURDATE() and (time >='22:00' and time <= '23:59')
THEN
SELECT count(*) FROM fax
WHERE ((date = CURDATE() and (time >='22:00' and time <= '23:59'))
or (date=CURDATE() + INTERVAL 1 DAY and (time >= '00:00' and time <= '7:00')))
and shift = 'GY' and complete = 1
ELSE IF
(date = CURDATE() and (time >= '00:00' and time <= '7:00')
THEN
SELECT count(*) FROM fax
WHERE ((date = CURDATE() and (time >= '00:00' and time <= '7:00'))
or (date=CURDATE() - INTERVAL 1 DAY and (time >='22:00' and time <= '23:59')))
and shift = 'GY' and complete = 1

Combine SUB_DATE AND TIMEDIFF to substract 1 hour in mysql

I'm having problems with timediff and DATE_SUB. Here is my MYSQL query:
SELECT id, clock_user_id, clock_date,
(Select clock_time from aura_clock where aura_clock.clock_type = 'Start' and
aura_clock.clock_date = t1.clock_date) as start, (Select clock_time
from aura_clock where aura_clock.clock_type = 'Stop'
and aura_clock.clock_date =
t1.clock_date) as Stop,
THE PROBLEM START HERE
TIMEDIFF((select clock_time FROM aura_clock t
WHERE t.clock_date = t1.clock_date AND t.clock_time > t1.clock_time
ORDER BY t.clock_time LIMIT 1), MIN(clock_time)) as spent
FROM aura_clock t1 WHERE t1.clock_date >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL
WEEKDAY(CURDATE()) DAY),INTERVAL 15 day)
AND t1.clock_date < DATE_SUB(curdate(),INTERVAL DAYOFWEEK(curdate()) + 6 day)
GROUP BY clock_date
The result is :
Now, I want to subtract 1 hour from the time spent using DATE_SUB but it didn't work.
as mirkobrankovic wrote:
((TIMEDIFF((select clock_time FROM aura_clock t
WHERE t.clock_date = t1.clock_date AND t.clock_time > t1.clock_time
ORDER BY t.clock_time LIMIT 1), MIN(clock_time))) - INTERVAL 1 HOUR) AS new_spent
should work
EDIT:
The best i got is time in seconds :
http://sqlfiddle.com/#!2/18160/66/0
a lot depends on MYSQL version