how to show only since and until date data - php

I have the next sql code
SELECT date,count(clics)
FROM advertising_clics
WHERE date > CURDATE() - INTERVAL 7 DAY
GROUP BY date
The Result is
date count(clics)
2015-08-29 4
2015-08-30 1
2015-08-31 1
2015-09-01 1
2015-09-02 1
but I want the result to be this
date count(clics)
2015-08-29 4
2015-09-02 1

try getting first record and last record and union both the results as
SELECT date,count(clics)
FROM advertising_clics
WHERE date > CURDATE() - INTERVAL 7 DAY
GROUP BY date
order by date
limit 1
union
SELECT date,count(clics)
FROM advertising_clics
WHERE date > CURDATE() - INTERVAL 7 DAY
GROUP BY date
order by date desc
limit 1

Related

Select value from moday this week on 18:00

I'm trying to get values from MySQL only on monday current week and from 18:00 to 19:00. How I can do this?
The result should be: 1
m_z_analytics
|id|m_type|date_added |
|1 |test1 |2018-06-25 18:02:09|
|2 |test2 |2018-06-26 19:44:24|
SELECT COUNT(id) AS id FROM m_z_analytics WHERE
Week(date_added) = Week(CURRENT_TIMESTAMP) AND
date_added >= (NOW() - INTERVAL 7 DAY) AND
date_added < (NOW() - INTERVAL 6 HOUR)
Use the WEEKDAY() or DAYNAME() function to test the day of week and HOUR() to test the time of day.
SELECT COUNT(*)
FROM m_z_analytics
WHERE date_added BETWEEN NOW() - INTERVAL 1 WEEK AND NOW() - INTERVAL 6 HOUR
AND HOUR(date_added) = 18
AND DAYNAME(date_added) = 'Monday'
You don't need the WEEK() function, since the time interval narrows to the current week.

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

Mysql query based on date?

I have a table like this
id | date | content
1 | 09-16-2013 | content 1 here
2 | 09-23-2013 | content 2 here
3 | 09-30-2013 | content 3 here
I would like to display the content for a week from that date. For example, the first content should start on 9/16/2013 and then show until 9/22/2013 mid night. then on next day, it changes to the content 2.
Same way,when I am on content 2, I want to display like "previous week content" and then show just the previous ones..I think I can do this by checking the current date and then anything below that has to be displayed.
I am not very good at these kind of mysql queries, please advise!
Regards
I guess you're looking for something like this
SELECT *
FROM table1
WHERE date BETWEEN CURDATE() + INTERVAL 0 - WEEKDAY(CURDATE()) DAY
AND CURDATE() + INTERVAL 6 - WEEKDAY(CURDATE()) DAY
This query will grab a row(s) where date column is within the boundaries of the current calendar week (from Monday to Sunday).
WEEKDAY() function returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday). The expression
CURDATE() + INTERVAL 0 - WEEKDAY(CURDATE()) DAY
returns a date for Monday of the current calendar week and
CURDATE() + INTERVAL 6 - WEEKDAY(CURDATE()) DAY
returns a date for Sunday of the current calendar week.
Using BETWEEN in WHERE clause makes sure that a query returns only rows with date values that falls between these two dates (Monday through Sunday).
Note: Make sure that you have an index on date column. This query is index-friendly.
Sample output for today's date (09/19/2013):
+------+------------+----------------+
| id | date | content |
+------+------------+----------------+
| 1 | 2013-09-16 | content 1 here |
+------+------------+----------------+
UPDATE: To get records for previous calendar week you just substract 1 week interval from both values in BETWEEN
SELECT *
FROM table1
WHERE date
BETWEEN CURDATE() + INTERVAL 0 - WEEKDAY(CURDATE()) DAY - INTERVAL 1 WEEK,
AND CURDATE() + INTERVAL 6 - WEEKDAY(CURDATE()) DAY - INTERVAL 1 WEEK
Try this
SELECT * FROM table WHERE date BETWEEN '09-16-2013' AND '09-22-2013';
keyword is WEEK()
SELECT id,date, CONCAT('content ',WEEK(date),' to here') as content FROM table_name
SELECT *
FROM table
WHERE date BETWEEN '9/16/2013 00:00:00.00' AND '9/22/2013 00:00:00.00'
You can replace the week offset to your needs
SET #weekOffset = +2;
SELECT * FROM test
WHERE WEEK(`date`) = WEEK(NOW()) + #weekOffset;
See a working demo here
To select it dynamically, try something like
SELECT * FROM `yourTable` WHERE NOW() >= STR_TO_DATE(`date`, '%m-%d-%Y') ORDER BY STR_TO_DATE(`date`, '%m-%d-%Y') DESC LIMIT 1
or t
SELECT * FROM `yourTable` WHERE CURDATE() >= STR_TO_DATE(`date`, '%m-%d-%Y') ORDER BY STR_TO_DATE(`date`, '%m-%d-%Y') DESC LIMIT 1
sqlfiddle example - http://sqlfiddle.com/#!2/62982/4

Trying to find out subscription end month notification

Hi i have this mysql table
id amount substart years subend
1 200 2012-01-10 1 2013-01-09
2 250 2012-02-15 2 2014-02-14
3 100 2012-02-11 1 2013-02-10
4 260 2012-03-22 3 2015-03-21
What i want is that to give notification a month before the end date. The current query is:
select count(subend) as count,curdate()
from subdur where status='active'
and (date_sub(subend,interval 1 month))>=curdate()
and (date_sub(subend,interval 1 month))<date_add(curdate(),interval 1 month)
order by subend;
The query is not giving me proper answer.
Thanks in advance
Try this::
select
count(subend) as count,
curdate()
from subdur
where status='active' and
subend BETWEEN (date_sub(curdate(),interval 1 month)) and curdate()
order by subend
Another way would be using date_diff:
select count(subend) as count, curdate()
from subdur
where status='active'
and date_diff(subend, curdate()) = 30 // >= 30 days or more, = 30 days exact
order by subend
;

First and last records from group by. Cannot get last

I have a table
Date value1 value2
2012-09-07 1 1
2012-09-06 2 2
2012-09-05 3 3
2012-09-04 4 4
2012-09-03 5 5
2012-08-31 6 6
2012-08-30 7 7
2012-08-29 8 8
2012-08-28 9 9
2012-08-27 10 10
2012-08-24 11 11
2012-08-23 12 12
2012-08-22 13 13
values in the table is not ascending like in example. There are random numbers.
I need to get the date of the week start, value1 on the beginning of the week and value2 at the end of the week.
Date field is unique, and it's stores day dates only so no duplicate dates are allowed.
I tried to use the query below:
SELECT MIN(`Date`) as Date,
(SELECT `value1` ORDER BY `Date` ASC LIMIT 1) as Start,
(SELECT `value2` ORDER BY `Date` DESC LIMIT 1) as End
FROM table
GROUP BY YEAR(`Date`), WEEK(`Date`,7)
The query returns grouped weeks and value1 correctly but value2 is also from the row of the week start i.e.
2012-08-27 10 10
but I need:
2012-08-27 10 6
What do I do wrong?
How about something like this
SELECT `date`, value1 as Start,
(SELECT value2 FROM photos WHERE t.date >= adddate(`Date`, INTERVAL 1-DAYOFWEEK(`Date`) DAY) AND t.date <= adddate(`Date`, INTERVAL 7-DAYOFWEEK(`Date`) DAY) ORDER BY date DESC LIMIT 1) as endDate
from table t
GROUP BY YEAR(`Date`), WEEK(`Date`,7)
There may be a more optimal way to do it.. but this works

Categories