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
;
Related
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
I have an example
id projectid date
1 2 2015-04-19
2 2 2015-04-19
3 2 2015-04-19
4 2 2015-04-19
5 2 2015-04-21
6 2 2015-04-21
7 2 2015-04-21
8 2 2015-04-22
9 2 2015-04-22
10 2 2015-04-22
I want to show next upcoming stored date using 2015-04-19 date, i.e next date is 2015-04-21, using mysql query
id projectid date
5 2 2015-04-21
6 2 2015-04-21
7 2 2015-04-21
I have written query for you. I hope it will resolve your problem.
QUERY
SELECT * FROM table
WHERE `date` = (SELECT `date` FROM table
WHERE `date`>'2015-04-19' ORDER BY `date` ASC LIMIT 1);
Use DateAdd Function in Select Statement like below
DATEADD(d,1,(CASE table.date as Date))
Basically to display next date older than 2015-04-19 from your database, this should be enough:
SELECT * FROM table WHERE date > '2015-04-19' ORDER BY date ASC LIMIT 1
SELECT * FROM table WHERE date > '2015-04-19' ORDER BY date LIMIT 1
UPDATE
You wrote that you want to show next upcoming stored date, but looking at your example output and your comment it looks like you want to get all records with the next date.
SELECT * FROM table WHERE date=(
SELECT date FROM table WHERE date > '2015-04-19' ORDER BY date LIMIT 1
)
SQL Fiffle example, use it as a copy paste solution.
Sub query will return the next date from which results will come according to that date only.
SELECT * FROM tableName WHERE date=("SELECT date FROM table WHERE date > '2015-04-19' ORDER BY date LIMIT 1")
I have a table as:
date passed failed subject
2015-2-1 2 1 maths
2015-2-1 3 2 cs
2015-3-1 1 2 maths
2015-12-1 2 1 maths
I have a form to select the start date and end date.
for eg:If I select startdate = 2015-2-1 and enddate=2015-3-1
my output should be:
date passed failed
2015-2-1 5 3
2015-3-1 1 2
output should contain the total no of passed and failed (irrespective of subjects) for same months as shown above.
This is the query which I used:
startdate=2015-2-1 and enddate=2015-3-1
SELECT SUM(passed) ,SUM(failed) FROM student_log WHERE
(DATE(`date`)='2015-2-1')
union
SELECT SUM(passed) ,SUM(failed) FROM student_log WHERE
(DATE(`date`)='2015-3-1')
It works fine But if my end date is December I should repeat my union query till December...
Please suggest an alternative.
What about:
SELECT SUM(passed), SUM(failed), DATE(`date`)
FROM student_log
WHERE DATE(`date`) >= '2015-2-1' AND DATE(`date`) <= '2015-3-1'
GROUP BY DATE(`date`);
This would find all tests between two dates and give you a count from each day.
You dont need separate queries or join or union for this
SELECT SUM(`pass`) as total_pass, SUM(`fail`) as total_fail
FROM pass_fail
WHERE (`date` = '2015-2-1' or `date` = '2015-3-1' )
GROUP BY `date`
Output
Refer Group By
this query :
Select date,sum (passed),sum(failed) from Results group by date
returns the following results for me :
date passed failed
2015-02-01 5 3
2015-03-01 1 2
2015-12-01 2 1
In other words: The group by clause will display the data per Month as you need it.
Going to make a line graph. I want to query count with difference of 5 days.
Suppose if I have following rows:
booking_date
2015-02-1
2015-02-3
2015-02-5
2015-02-6
2015-02-6
2015-02-9
2015-02-10
2015-02-15
2015-02-17
2015-02-23
2015-02-28
In above table column it contains date. Now How can I do mysql query so that it can return with difference of 5 days like:
1 => 3 // count of date between 2015-02-1 & 2015-02-05 is 3
2 => 4 // count of date between 2015-02-06 & 2015-02-10 is 4
3 => 1 // count of date between 2015-02-11 & 2015-02-15 is 1
4 => 1 // count of date between 2015-02-16 & 2015-02-20 is 1
5 => 1 // count of date between 2015-02-21 & 2015-02-25 is 1
6 => 1 // count of date between 2015-02-26 & 2015-02-30 is 1
Is any direct way to query like above. I am not so good at mysql. But can do php nicely.
You can do the following to get everything in the same query.
First of all get the unix timestamp for the date you want to start grouping in 5 days. In your example that would be 2015-02-01 -> 1422748800
Then the query would be the following:
SELECT COUNT(*), FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5)) as FiveDayPackNumber from tbl GROUP BY FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5))
Haven't tested it so it may require some tweaking but you can get the idea: It will group them by the number of 5-days-packs that passed since your initial date, starting at 0.
Do you mean to do something like:
SELECT COUNT(1) FROM {table_name} WHERE booking_date > 2015-02-01 AND booking_date < 2015-02-05
?
(completely off memory so you might need to slightly alter it)
SELECT count(*)
FROM tbl
WHERE booking_date >= start_date
AND booking_date <= end_date
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