MySQL query - select entries not older than a time period - php

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
?

Related

How to edit my query to present the output per day?

Instead of returning 5 rows, how can I edit the following query to return
date --- rows found
date --- rows found
date --- rows found
here is the query
SElECT * FROM emails WHERE maildate >= DATE_ADD(CURDATE(), INTERVAL -3 DAY) and company_id = 1 order by maildate desc
Group on maildate and COUNT(*) should work:
SELECT maildate, COUNT(*) as TotalEmails --change alias as desired
FROM emails
WHERE maildate >= DATE_ADD(CURDATE(), INTERVAL -3 DAY)
AND company_id = 1
GROUP BY date(maildate) --Remove timestamp for grouping
ORDER BY maildate desc
Although it's unclear what you want returned. Sample data and expected output would help.

how can i write as one query to get count in mysql

I want to get the today count of users and yesterday's users count for that i want to write only one query how can i do that..?
these are my queries I want only one query:
SELECT COUNT(*) FROM visitors group by visited_date ORDER by visited_date DESC limit 1,1 as todayCount
SELECT COUNT(*) FROM visitors group by visited_date ORDER by visited_date DESC limit 1,0 as yesterdayCount
My expected results or only 2 columns
todayCount yesterdayCount
2 4
This should do the trick:
SELECT COUNT(CASE
WHEN visited_date = CURDATE() THEN 1
END) AS todayCount ,
COUNT(CASE
WHEN visited_date = CURDATE() - INTERVAL 1 DAY THEN 1
END) AS yesterdayCount
FROM visitors
WHERE visited_date IN (CURDATE(), CURDATE() - INTERVAL 1 DAY)
GROUP BY visited_date
ORDER by visited_date
If you know the current and previous date, then you can do:
SELECT SUM(visited_date = CURDATE()) as today,
SUM(visited_date = CURDATE() - interval 1 day) as yesterday
FROM visitors
WHERE visited_date >= CURDATE() - interval 1 day;
If you don't know the two days, then you can do something similar, getting the latest date in the data:
SELECT SUM(v.visited_date = m.max_vd) as today,
SUM(v.visited_date < m.max_vd) as yesterday
FROM visitors v CROSS JOIN
(SELECT MAX(v2.visited_date) as max_vd FROM visitors v2) as m
WHERE v.visited_date >= m.max_vd - interval 1 day
Just try this simple query
select visited_date as date, COUNT(*) as count from `visitors`
group by `visited_date` order by `visited_date` asc
It will produce output as
It will work for you.
Try this:
$sqlToday = "Select COUNT(*) FROM menjava WHERE DATE(date_submitted)=CURRENT_DATE()";
$sqlYesterday = "Select COUNT(*) FROM menjava WHERE DATE(dc_created) = CURDATE() - INTERVAL 1 DAY";

MYSQL Last month Query is not returning first 9 days

I am querying records from the last calendar month. As it is February, it should return all the records that were added on January this year.
My Query:
`SELECT * FROM table_name WHERE date >=
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1
DAY) AND date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1
MONTH)), INTERVAL 0 DAY) AND campaign = '$campaign' ORDER BY date
ASC`
It returns some records but skips the first 9 days. It starts showing records from 10th of the previous month. What am I missing here?
check your date field type and make sure you have not mistaken it with varchar.
SELECT * FROM table_name WHERE
(MONTH(date) = (MONTH(NOW()) - 1) AND YEAR(date) = YEAR(NOW()))
OR
(MONTH(date) = 12 AND MONTH(NOW())=1 AND YEAR(date) = (YEAR(NOW()) - 1) AND campaign = '$campaign' ORDER BY date
ASC`
Try To Get Data in step by step like,
First, you should try below query.
SELECT Date, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AS StartDate, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY) AS EndDate FROM MyTable
Second, If First Step give right date then get your data by directly writing your date rather than DATE_ADD function in where clause.
Third, If These will Give you write DATA then try to fetch data using DATE_ADD function.
Replay If you will get solution.
SELECT * FROM table_name WHERE date between DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 MONTH) AND DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 DAY) AND campaign = '$campaign' ORDER BY date ASC

get upcoming date record from mysql database, In codeigniter

I am working on an event management project, I need to get upcoming event from database, can anyone help me ?
I am using
SELECT *FROM EVENTS WHERE DATE_ADD(event_date, INTERVAL YEAR(CURDATE())-YEAR(event_date) YEAR)
BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY) LIMIT 1;
But it gives me the event upcoming within 7 days only.
I should not want to provide days.
I want to get first upcoming event , doesn't matter after or within how many days it is coming.
thank you !
Isn't this a simple where clause comparing with current date
SELECT *
FROM EVENTS
WHERE event_date > CURDATE()
ORDER BY event_date
LIMIT 1;
Wouldn't this work?
SELECT * FROM EVENTS WHERE event_date > CURDATE() ORDER BY event_date ASC LIMIT 1;
try
If column type datetime you can use CURDATE()(mysql function) or date()(php function)
"SELECT *FROM EVENTS WHERE event_date > '".date('Y-m-d H:i:s')."' ORDER BY event_date limit 1"
or
"SELECT *FROM EVENTS WHERE event_date > CURDATE() ORDER BY event_date limit 1"
If column type date
"SELECT *FROM EVENTS WHERE event_date > '".date('Y-m-d')."' ORDER BY event_date limit 1"
try below it works perfectly fine
select * from table where start_time between NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)
you can use like this to get next 7 days data or in case you want last 7 days then instead of DATE_ADD USE DATE_SUB It will work or you can use like
start_time between NOW() AND DATE_ADD(NOW(), INTERVAL 1 WEEK)
this also works. to know more check this ref https://dev.mysql.com/doc/en/date-and-time-functions.html
thank you everyone,
I got the answer:
SELECT * FROM EVENTS
WHERE event_date > CURDATE()
ORDER BY event_date ASC LIMIT 1;

query to select data from DB based on a particular day and count how many rows are there?

I have Db which has data of an year...so I need a query to select data from based on a date(particularly yesterday's) and count how many are in this.....
Help please!
It has a column with name "created" where all the date & time are present.
code used:
//$thirty_reg = mysql_query("SELECT column FROM user WHERE user.date BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()");
//$num_thirty=mysql_num_rows($thirty_reg);
//echo $num_thirty;
select *
from YourTable
where date_sub(curdate(), interval 1 day) < DateColumn
and DateColumn < curdate()
To fetch result set:
SELECT * FROM table WHERE data_date = DATE_SUB(NOW(), INTERVAL 1 DAY)
To fetch count of rows:
SELECT COUNT(*) FROM table WHERE data_date = DATE_SUB(NOW(), INTERVAL 1 DAY)

Categories