Hello I have a table for football games, and I need to get last game and next game based on today's date.
I have tried to use this query
SELECT DATE( match_date ) AS yesterday
FROM matches
WHERE DATE( match_date ) = DATE( DATE_SUB( NOW( ) , INTERVAL 0 DAY ) )
GROUP BY yesterday
but the record can be before 2 days or 3 etc...
Also, for the Next Game If I use tomorrow date, I will not be sure if the game is exist on the next day or after.
I used this query to get all the games before today's date.
SELECT *
FROM matches
WHERE DATE( match_date ) < DATE( DATE_SUB( NOW( ) , INTERVAL 0 DAY ) )
What I need is if Today is 12-26-2012, and the table like this
----------------------------------------------
id Date Home Away
----------------------------------------------
1 2012-12-26 23 85
2 2012-12-25 11 23
3 2012-12-01 23 43
4 2012-12-29 14 23
5 2013-01-14 23 192
6 2013-01-17 23 77
INPUT: GET THE NEXT GAME FOR TEAM = 23
OUTPUT: 4
INPUT: GET THE LAST GAME FOR TEAM = 23
OUTPUT: 3
To get the last game:
SELECT DATE( match_date ) AS last_game
FROM matches
WHERE DATE( match_date ) < CURDATE()
ORDER BY match_date DESC
LIMIT 1;
To get the next game:
SELECT DATE( match_date ) AS last_game
FROM matches
WHERE DATE( match_date ) > CURDATE()
ORDER BY match_date ASC
LIMIT 1;
Related
My below query is giving me result of top videos played in past 2 hours, but my requirement is to also get top videos of past 4 hours to past 2 hours, for example if by this query I am getting data from 01:00 PM to 03:00 PM, I also want data from 09:00 AM to 01:00 PM. Can I do this in one query and in efficient way.
Query:
select SQL_CACHE channel,SUBSTRING_INDEX(GROUP_CONCAT(video_id ORDER BY plays DESC),',', 40) AS video_ids,now() as datetime from
(SELECT channel,video_id,count(video_id) as plays FROM `tbl`
WHERE `datetime_col` > DATE_SUB( now(), INTERVAL 2 HOUR )
and channel != 0
and cat_id != 8
group by channel,video_id
order by channel,plays DESC)x
group by channel;
Thanks in advance.
You can use the same query with UNION to group the results. Also, you can use BETWEEN to define the intervals, e.g.
select SQL_CACHE channel,SUBSTRING_INDEX(GROUP_CONCAT(video_id ORDER BY plays DESC),',', 40) AS video_ids,now() as datetime from
(SELECT channel,video_id,count(video_id) as plays FROM `tbl`
WHERE `datetime_col` BETWEEN DATE_SUB( now(), INTERVAL 2 HOUR ) AND NOW()
and channel != 0
and cat_id != 8
group by channel,video_id
order by channel,plays DESC)x
group by channel
UNION
select SQL_CACHE channel,SUBSTRING_INDEX(GROUP_CONCAT(video_id ORDER BY plays DESC),',', 40) AS video_ids,DATE_SUB( now(), INTERVAL 2 HOUR ) as datetime from
(SELECT channel,video_id,count(video_id) as plays FROM `tbl`
WHERE `datetime_col` BETWEEN DATE_SUB( now(), INTERVAL 4 HOUR ) AND DATE_SUB( now(), INTERVAL 2 HOUR )
and channel != 0
and cat_id != 8
group by channel,video_id
order by channel,plays DESC)x
group by channel;
I am trying to create a mysql query that will show when the teams score their goals during the matches. I have a table that is setup like this: clubid(int) and minute(tinyint). This what I want to achieve:
Minute 1-15 31 goals
Minute 16-30 43 goals
Minute 31-45 36 goals
Minute 46-60 51 goals
Minute 61-75 48 goals
Minute 76-90 52 goals
First I would like to do it for every team in the league and then for each team separate. This is my attempt of the query for the whole league, but unfortunaly it doesn´t work.
SELECT *,count(minute) as summary
CASE WHEN minute >= 1 AND minute <= 15 THEN '1-15'
WHEN minute >= 16 AND minute <= 30 THEN '16-30'
WHEN minute >= 31 AND minute <= 45 THEN '31-45'
WHEN minute >= 46 AND minute <= 60 THEN '46-60'
WHEN minute >= 61 AND minute <= 75 THEN '61-75'
WHEN minute >= 76 AND minute <= 90 THEN '76-90'
WHEN minute > 90 AND THEN 90+
ELSE 'no goals' END as range
FROM teamgoals
ORDER BY range asc
Thank you in advance for any help!
Please try this sqlFiddle
SELECT IFNULL(T1.summary,0) as summary,
rangeDescription.`range`
FROM ( SELECT '1-15' as `range`, 1 as rowOrder
UNION SELECT '16-30' , 2
UNION SELECT '31-45' , 3
UNION SELECT '46-60' , 4
UNION SELECT '61-75' , 5
UNION SELECT '76-90' , 6
UNION SELECT '90+' , 7
)rangeDescription
LEFT JOIN
(SELECT count(minute) as summary,
CASE WHEN minute >= 1 AND minute <= 15 THEN '1-15'
WHEN minute >= 16 AND minute <= 30 THEN '16-30'
WHEN minute >= 31 AND minute <= 45 THEN '31-45'
WHEN minute >= 46 AND minute <= 60 THEN '46-60'
WHEN minute >= 61 AND minute <= 75 THEN '61-75'
WHEN minute >= 76 AND minute <= 90 THEN '76-90'
WHEN minute > 90 THEN '90+'
ELSE 'no goals'
END as `range`
FROM
teamgoals
WHERE clubId = 1
GROUP BY `range`
)T1
ON rangeDescription.`range` = T1.`range`
ORDER BY rangeDescription.rowOrder asc
I have created a table emp_info with email,mobile, timestamp as fields.
I want to retrieve last 1 week record on per day basis. And for this I have tried
SELECT count(*)
FROM `emp_info`
WHERE DATE(timestamp ) > DATE_SUB( CURDATE( ) , INTERVAL 1 WEEK )
ORDER BY saved_timestamp
and it shows total no. of records entered in last 7 days which is not my desired out put.
So I want the out put of records for 7 days like:
Day count
Monday 2
Tuesday 0
.... ..
.... ..
So somebody please help me out?
Just add the DAYNAME to your column list and aggregate.
SELECT DAYNAME(timestamp), COUNT(*)
FROM `emp_info`
WHERE DATE(timestamp ) > DATE_SUB( CURDATE( ) , INTERVAL 1 WEEK )
GROUP BY DAYNAME(timestamp)
ORDER BY saved_timestamp
Try to extract the day of week from the date then use the count and group it by the day of the week and you can get the count to each of the day.
SELECT DAYOFWEEK(DATE(timestamp)), count(*) FROM `emp_info` WHERE DATE(timestamp ) > DATE_SUB( CURDATE( ) , INTERVAL 1 WEEK ) group by DAYOFWEEK(DATE(timestamp))
I have table like
CREATE TABLE `survey` (
`id` int(11) NOT NULL auto_increment,
`submitdate` datetime default NULL,
`answer` varchar(5) collate utf8_unicode_ci default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=499 ;
now to get values like
c t Clicks
2012-10-29 2012-10-22 10
2012-11-04 2012-10-30 20
2012-11-11 2012-11-05 30
2012-11-19 2012-11-12 34
I am using this query
SELECT uq.timespan, COALESCE(tsq.TotalClicks, 0) as Clicks FROM (
SELECT DATE( DATE_ADD( NOW( ) , INTERVAL -21
DAY ) ) c, DATE( DATE_ADD( NOW( ) , INTERVAL -28
DAY ) ) l
union SELECT DATE( DATE_ADD( NOW( ) , INTERVAL -15
DAY ) ) c, DATE( DATE_ADD( NOW( ) , INTERVAL -20
DAY ) ) l
union SELECT DATE( DATE_ADD( NOW( ) , INTERVAL -8
DAY ) ) c, DATE( DATE_ADD( NOW( ) , INTERVAL -14
DAY ) ) l
union SELECT curdate() c,DATE( DATE_ADD( NOW( ) , INTERVAL -7
DAY ) ) l
)uq LEFT JOIN (
SELECT CASE
WHEN submitdate >= NOW() - INTERVAL 4 WEEK
AND submitdate < NOW() - INTERVAL 3 WEEK THEN c 'to' l
DAY ) )
WHEN submitdate >= NOW() - INTERVAL 3 WEEK
AND submitdate < NOW() - INTERVAL 2 WEEK THEN c 'to' l
WHEN submitdate >= NOW() - INTERVAL 2 WEEK
AND submitdate < NOW() - INTERVAL 1 WEEK THEN c 'to' l
DAY ) )
WHEN submitdate >= NOW() - INTERVAL 1 WEEK THEN c 'to' l
END Weeksubmitdate,
count(id) TotalClicks
FROM survey
WHERE submitdate >= NOW() - INTERVAL 4 WEEK
GROUP BY Weeksubmitdate
)tsq ON uq.timespan = tsq.Weeksubmitdate";
problem is with 16th line c to l.
I am getting the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''to' l
DAY ) )
WHEN submitdate >= NOW() - INTERVAL 3 WEEK
' at line 16
CASE is supposed to evaluate to a scalar expression. That means its THEN clauses must evaluate to scalar expressions too. Now, what does this c 'to' l thing stand for? Is it a scalar expression? It doesn't seem one to me, however I may be unaware of some things in MySQL, so it's more important whether MySQL itself recognises that as a scalar expression. And apparently it doesn't.
There is another issue. You are trying to reference a derived table's columns inside another derived table. More specifically, you seem to be trying to reference the columns c and l of uq inside the tsq subselect, and that is illegal. If uq was a normal table, it would be fine, but since it is a virtual table, the query doesn't know about its existence at that point, i.e. at the time of parsing the tsq subquery.
Anyway, what you seem to be doing with your query could probably be rewritten more simply, like this, for instance:
SELECT
MIN(submitdate) AS startdate,
MAX(submitdate) AS enddate,
COUNT(*) AS clicks
FROM (
SELECT
CASE
WHEN submitdate >= NOW() - INTERVAL 1 WEEK THEN 1
WHEN submitdate >= NOW() - INTERVAL 2 WEEK THEN 2
WHEN submitdate >= NOW() - INTERVAL 3 WEEK THEN 3
WHEN submitdate >= NOW() - INTERVAL 4 WEEK THEN 4
END AS weekid,
*
FROM survey
) s
GROUP BY
weekid
ORDER BY
startdate
;
The subquery assigns surrogate week IDs to every row of survey. The main query groups the results by those IDs and produces the counts as well as starting & ending dates for every group.
I have a table that looks like this:
ID t_stamp views uviews hits uhits
1 7/18/2012 19:00 105 11 0 0
5 7/18/2012 20:00 1 1 0 0
2 7/19/2012 9:00 118 4 0 0
1 7/19/2012 10:00 196 18 0 0
7 7/19/2012 11:00 2 1 0 0
2 7/19/2012 12:00 38 11 0 0
2 7/19/2012 13:00 20 5 0 0
2 7/19/2012 19:00 9 2 0 0
2 7/20/2012 15:00 85 6 0 0
1 7/20/2012 16:00 483 101 2 2
2 7/20/2012 17:00 1200 240 0 0
2 7/20/2012 18:00 1200 232 0 0
2 7/20/2012 19:00 1199 231 0 0
2 7/20/2012 20:00 1200 236 0 0
2 7/20/2012 21:00 1201 237 0 0
1 7/20/2012 22:00 1220 187 0 0
1 7/20/2012 23:00 869 165 0 0
And my method is to combine them by the day so I can get a SUM for each of the last four columns. The IDs do not really matter.
I am using this:
SELECT `bannerID` , DATE_FORMAT( `t_stamp` , '%m/%d/%Y' ) AS `date` ,
SUM( `views` ) AS `views` , SUM( `uviews` ) AS `uviews` , SUM( `hits` ) AS `hits` , SUM( `uhits` ) AS `uhits`
FROM test_bannerstats
WHERE DATE( t_stamp ) >= DATE( '2012-07-01' )
AND DATE( t_stamp ) <= DATE( '2012-08-24' )
GROUP BY `date`
ORDER BY `date` ASC
However that doesn't seem correct to me as that the numbers seem conflicting. In the end I want to get a daily tally the last four columns by day.
EDIT:
It is a problem with time zones it looks! I will show you why...
Look at the table above, now let's do the additions for the entire day...
1 07/18/2012 106 12 0 0
1 07/19/2012 383 41 0 0
1 07/20/2012 8657 1635 2 2
Above is correct. Below is wrong.
1 07/18/2012 105 11 0 0
1 07/19/2012 384 42 0 0
1 07/20/2012 4167 810 2 2
The problem? Anything after 8pm is going to the next day. It is a timezone issue that I have to sort out it seems.
Your query can be simplified as:
SELECT `bannerID`,
DATE_FORMAT( `t_stamp` , '%m/%d/%Y' ) AS `date`,
SUM( `views` ) AS `views`,
SUM( `uviews` ) AS `uviews`,
SUM( `hits` ) AS `hits`,
SUM( `uhits` ) AS `uhits`
FROM test_bannerstats
WHERE DATE( t_stamp ) BETWEEN '2012-07-01' AND '2012-08-24'
GROUP BY DATE(t_stamp)
ORDER BY DATE(t_stamp) ASC;
try this one,
SELECT DATE_FORMAT(DATE(t_stamp), '%m/%d/%Y') AS `date`,
SUM(views) totalViews,
SUM(uviews) totalUViews,
SUM(hits) totalHits,
SUM(uhits) totalUHits,
FROM tableName
WHERE DATE( t_stamp ) >= DATE( '2012-07-01' ) AND
DATE( t_stamp ) <= DATE( '2012-08-24' )
GROUP BY DATE(t_stamp)
ORDER BY `date` ASC
I think you're sql query wont really work, that is only applicable for date july 1 to august 24. What if the last 4 date is beyond that date? It wont pick up anything.
In my opinion the best way to do it is this:
SELECT SUM(column_name) FROM table_name order by id DESC limit 4;
Order by ID desc will sort the results in descending order. Limit 4, will only pick up the first 4 results.
Hope that helps.
If you want to give your indexes a chance to be used, avoid using functions on columns when you can. Replace the conditions:
WHERE DATE( t_stamp ) >= DATE( '2012-07-01' )
AND DATE( t_stamp ) <= DATE( '2012-08-24' )`
with:
WHERE t_stamp >= DATE( '2012-07-01' )
AND t_stamp < DATE( '2012-08-25' )
You define the alias date in the SELECT list. This alias cannot be used in the WHERE or GROUP BY clauses (to be honest, it can be used at the GROUP BY clause but I wouldn't recommend it). Instead of:
GROUP BY `date`
use:
GROUP BY DATE(t_stamp)
There is a proprietary MySQL syntax available (you can read it at the SELECT documenation) when the GROUP BY and ORDER BY are done on the same expression. Instead of:
GROUP BY DATE(t_stamp)
ORDER BY DATE(t_stamp) ASC;
you can use (for a slight efficiency gain):
GROUP BY DATE( t_stamp ) ASC ;
The query becomes now:
SELECT
bannerID
, DATE_FORMAT( DATE( t_stamp ), '%m/%d/%Y' ) AS `date`
, SUM( views ) AS views
, SUM( uviews ) AS uviews
, SUM( hits ) AS hits
, SUM( uhits ) AS uhits
FROM
test_bannerstats
WHERE
t_stamp >= DATE( '2012-07-01' )
AND
t_stamp < DATE( '2012-08-25' ) --- notice the `<` and the +1 date offset
GROUP BY
DATE( t_stamp ) ASC ;