Selecting in between won't work with MySQL? - php

I have a quick and asap issue.
SELECT `deals`.*
FROM `deals`
WHERE
`is_featured` = 1 AND
`status` = 'active' AND
CURDATE() BETWEEN start_date AND DATE_ADD(end_date, INTERVAL 1 DAY)
ORDER BY `end_date` DESC
LIMIT 1
Is this right?
start_date is "2012-01-11 00:00:00" and end_date is "2012-01-11 23:59:59".
This is what I wish to show:
I want to show the dealoffer that is between the current datetime and if the is_featured is 1 and status are "active".
(CURDATE() gives me only date, not time, is this the problem? How can I get current datetime in MySql?).
It should only pick one deal and this deal should be the one with the closest end date from current datetime.

Here is your original query
SELECT `deals`.* FROM `deals`
WHERE `is_featured` = 1
AND `status` = 'active'
AND CURDATE() BETWEEN start_date AND DATE_ADD(end_date, INTERVAL 1 DAY)
ORDER BY `end_date` DESC
LIMIT 1;
Replace the BETWEEN with this
SELECT `deals`.* FROM `deals`
WHERE `is_featured` = 1
AND `status` = 'active'
AND start_date >= (CURDATE() + INTERVAL 0 SECOND)
AND start_date <= (end_date + INTERVAL 1 DAY)
ORDER BY `end_date` DESC
LIMIT 1;
and add this index
ALTER TABLE deals ADD INDEX (is_featured,status,start_date,end_date);

Use now() to get the date and time.

You could use the curtime() function to retrieve the current time.

Related

i have my value as 2018-05-03 11:02:00 in mysql i want to pass this in my where condition using php

In my table 'time' data is present on 2018-05-03 11:02:00 this datetime. So I want to get the data by passing this datetime.
Here is my code:
$query1=$this->db->query("SELECT sum(count) As total, sum(status) As
is_completed FROM `tasks` WHERE `staff_id` = '$datas[$i]' AND `time` >
DATE_SUB(CURDATE(), INTERVAL 1 HOUR)");
This code showing no results but there is a data present on this datetime. how
can i pass this datetime as where condition
CURDATE() returns a date only. So when you do DATE_SUB(CURDATE(), INTERVAL 1 HOUR), you are returning the equivalent of 23:00:00 Yesterday
It seems for what you are trying to do, it would be better to use NOW().
You can try the following query:
SELECT sum(count) AS total, sum(status) AS is_completed
FROM `tasks` WHERE `staff_id` = '$datas[$i]'
AND `time` > DATE_SUB(NOW(), INTERVAL 1 HOUR)
Change CUR_DATE() to NOW()
$query1=$this->db->query("SELECT sum(count) As total, sum(status) As is_completed FROM `tasks` WHERE `staff_id` = '$datas[$i]' AND `time` > DATE_SUB(NOW(), INTERVAL 1 HOUR)");

Database query needs a lot of time

I am currently running this query and it takes around 15 sec to load i want to optimize the query
SELECT *, `points`.`players` as `players` ,
FROM_UNIXTIME(`points`.`timestamp`, '%Y-%m-%d %H:%i') as `date`,
(SELECT `points`.`players`
FROM `points`
WHERE FROM_UNIXTIME(`points`.`timestamp`, '%Y-%m-%d %H:%i') > `date` - INTERVAL 7 DAY
AND FROM_UNIXTIME(`points`.`timestamp`, '%Y-%m-%d %H:%i') < `date` - INTERVAL 167 HOUR
AND `server_id` = {$server_id}
AND `type` = 2
LIMIT 1
) as `prevplayers`
FROM `points`
WHERE `points`.`timestamp` > UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)
AND `server_id` = {$server_id}
AND `type`=2
GROUP BY DATE_FORMAT(FROM_UNIXTIME(`points`.`timestamp`), '%Y-%m-%d %H')
ORDER BY DATE_FORMAT(FROM_UNIXTIME(`points`.`timestamp`),'%Y-%m-%d %H')
Table structure
Indexes
With Explain Output
Ideal time is 1-2 secs
Any Suggestions?
Thanks
Somdeb
Can you create an additional index with the columns in following order and retry the query?
server_id
type
timestamp (mention descending order)

Adjust mysql query to filter out today from the date

Hi all i currently have this sql:
SELECT a.*
FROM (SELECT a.*
FROM articles a
WHERE date >= UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 7 DAY)) AND a.active = 1
ORDER BY views ASC
) a
ORDER BY views ASC
It lists all articles posted in the last week, what I want to do is adjust it so it ignored today, is that easy to do?
Certainly. You just need to add AND date < UNIX_TIMESTAMP(CURDATE())
For simplicity, you can use the BETWEEN operator:
WHERE `date` BETWEEN UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 7 DAY))
AND UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 1 DAY))
I believe this allows the engine to make better use of indexes than individual >= and <= calls, but I'm not certain on that.
Shouldn't
SELECT `a`.*
FROM `articles` AS a
WHERE `date` >= UNIX_TIMESTAMP(NOW(TODAY() - INTERVAL 7 DAY)) AND `date` <= UNIXTIMESTAMP(DATE(NOW() - INTERVAL 1 DAY)) `a`.`active` = 1
ORDER BY `views` ASC
Suffice for this task?

MySQL query - select entries not older than a time period

I have this query
SELECT * FROM content
WHERE topic='$rw09[id]' AND active='1' AND date < DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
ORDER BY cpc DESC, id DESC
LIMIT 4
The key part of my query is date < DATE_SUB(CURDATE(), INTERVAL 1 WEEK). It returns me entries older than a week. What i want it to return me is entries NOT older than 1 week. How can i modify it to return me desired result?
Thank you.
Have you tried with
SELECT * FROM content
WHERE topic='$rw09[id]' AND active='1' AND date > DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
ORDER BY cpc DESC, id DESC
LIMIT 4
?

php - get different dates in while loop

I have this while loop:
<?php
$q=mysql_query("SELECT * FROM xeon_stats_clicks WHERE user='".$userdata['username']."' AND typ='4' ORDER BY data DESC LIMIT 8") or die(mysql_error());
while($clickData=mysql_fetch_assoc($q)):
$r=mysql_query("SELECT sum(value) FROM `xeon_stats_clicks` WHERE user='".$userdata['username']."' AND typ='3' AND data='".date("Y/m/d")."' ORDER BY data DESC LIMIT 8");
echo mysql_result($r, 0);
endwhile;
?>
This will just give me the sum of the value row for today only. How can I do so I get the data from the last 7 days?
there is no need to specify the certain date for it will give you on this specific date, another comment suggested the use of 'between' but I like the use of '>' only in this case,
for example:
SELECT sum(value) FROM xeon_stats_clicks WHERE
user=.$userdata['username'] AND type='3' AND date > CURDATE() -
INTERVAL 1 WEEK ORDER BY date DESC LIMIT 8"
SELECT SUM( value ), `data`
FROM table
WHERE ( `data` BETWEEN CURDATE() AND CURDATE() - INTERVAL 1 WEEK )
GROUP BY `data`
for example.
try another where statement:
DATEDIFF( CURDATE(), CURDATE() - INTERVAL 1 WEEK ) = 7
and show your where statement

Categories