If there are 2 columns in mysql database: year; month, now I want to do a sum calculation based a year-month range without specifying the date. Let's say 2010-11 to 2011-07, how can I realize it?
SELECT * FROM TT WHERE F1 BETWEEN '2010-11' AND '2011-07'
It doesn't work.
If you want to take all rows from 2010-11 to 2011-07, until the first day of August:
SELECT * FROM `table`
WHERE `date_column` BETWEEN '2010-11-01' AND '2011-08-01'
Use this query if you want to get all rows from the full months of January to June:
SELECT * FROM `table`
WHERE YEAR(`date_column`)=2011 AND MONTH(`date_column`) BETWEEN 1 AND 6
If you want to use different years, then write different queries for each year:
SELECT * FROM `table`
WHERE
(YEAR(`date_column`)=2010 AND MONTH(`date_column`) BETWEEN 11 AND 12) OR
(YEAR(`date_column`)=2011 AND MONTH(`date_column`) BETWEEN 1 AND 7)
Try this, if F1 is of type date
SELECT * FROM TT WHERE F1 BETWEEN '2010-11-01' AND '2011-07-31'
and this if F1 is of type datetime
SELECT * FROM TT WHERE F1 BETWEEN '2010-11-01 00:00:00' AND '2011-07-31 23:59:59'
if year and month are saved in different columns then use this
SELECT * FROM TT WHERE DATE(CONCAT(year_column, '-', month_column, '-01'))
BETWEEN '2010-11-01' AND '2011-07-31'
I came across to the same situation and successfully managed by doing this:
SELECT * FROM `TT` WHERE CONCAT(year_column,month_column) between '201011' and '201107';
hope it helps others also..
Related
RDBMS: MySQL
The time column(s) datatype is of datetime
For every hour of the 24 hour day I need to retrieve the number of rows in which their start_time matches the hour OR the end_time is great than or equal to the hour.
Below is the current query I have which returns the data I need but only based off of one hour. I can loop through and do 24 separate queries for each hour of the day but I would love to have this in one query.
SELECT COUNT(*) as total_online
FROM broadcasts
WHERE DATE(start_time) = '2018-01-01' AND (HOUR(start_time) = '0' OR
HOUR(end_time) >= '0')
Is there a better way of querying the data I need? Perhaps by using group by somehow? Thank you.
Not exactly sure if i am following, but try something like this:
select datepart(hh, getdate()) , count(*)
from broadcasts
where datepart(hh, starttime) <=datepart(hh, endtime)
and cast(starttime as date)=cast(getdate() as date) and cast(endtime as date)=cast(getdate() as date)
group by datepart(hh, getdate())
Join with a subquery that returns all the hour numbers:
SELECT h.hour_num, COUNT(*) AS total_online
FROM (SELECT 0 AS hour_num UNION SELECT 1 UNION SELECT 2 ... UNION SELECT 23) AS h
JOIN broadcasts AS b ON HOUR(b.start_time) = h.hour_num OR HOUR(b.end_time) >= h.hour_num
WHERE DATE(b.start_time) = '2018-01-01'
GROUP BY h.hour_num
Imagine a table with field 'datetime'. Example rows:
2017-01-27 13:06:02
2017-01-27 05:13:14
2017-01-23 22:13:56
2017-01-26 14:02:09
2017-01-23 13:26:12
...
I need to get * from the bold lines, BUT WITHIN the last 30 days from now...
In other words, rows with the max date in the last 30 days.
30 rows in total in each case, assuming every day has at least one row...
You can group by the date part of datetime and get the max for each day.
select max(`datetime`)
from tablename
where `datetime` >= date(now())-interval '30' day
group by date(`datetime`)
To get all the fields from the table for such rows, use
select * from tablename where `datetime` in (
select max(`datetime`)
from tablename
where `datetime` >= date(now())-interval '30' day
group by date(`datetime`)
)
vkp's answer is basically correct, although there's no need for subquery to select the final result from - you can just put other columns straight into your query, up to something like this:
select *, max(datetime)
from tablename
where datetime >= date(now())-interval '30' day
group by date(datetime);
Ah, and that works for joins too.
The other thing I'd change to address the goal more precise, is:
max(time(datetime))
select * from your_table
where datetime between sysdate and sysdate-30
My code:
$results = $GLOBALS['wpdb']->get_results( 'SELECT * FROM myTable WHERE date = 2014 ORDER BY id DESC', object );
The problem is date is stored in this format: 2014-01-01
So how do I select just the year ( I don't care about month and day for the time being ).
Thanks
Use the year() function:
WHERE year(date) = 2014
or use explicit comparisons:
WHERE (date >= '2014-01-01' and date < '2015-01-01')
The latter is better because it can make use of an index on the date column.
Try this Query :
SELECT * FROM myTable WHERE year(`date`)='2014' ORDER BY id DESC
Try this:
SELECT * FROM myTable WHERE date >= '2014-01-01 00:00:00' ORDER BY id DESC
To select all rows where the year of a date column (called date_col) is equal to 2014 use the year function:
SELECT * FROM `tbl` WHERE Year(`date_col`) = '2014';
You can select year for get posts with query_posts(); parameter is year. Example: query_posts("year=2014"); This is not full question for you, only alternative..
I have a table
id user Visitor timestamp
13 username abc 2014-01-16 15:01:44
I have to 'Count' total visitors for a 'User' for last seven days group by date(not timestamp)
SELECT count(*) from tableA WHERE user=username GROUPBY __How to do it__ LIMIT for last seven day from today.
If any day no visitor came so, no row would be there so it should show 0.
What would be correct QUERY?
There is no need to GROUP BY resultset, you need to count visits for a week (with unspecified user). Try this:
SELECT
COUNT(*)
FROM
`table`
WHERE
`timestamp` >= (NOW() - INTERVAL 7 DAY);
If you need to track visits for a specified user, then try this:
SELECT
DATE(`timestamp`) as `date`,
COUNT(*) as `count`
FROM
`table`
WHERE
(`timestamp` >= (NOW() - INTERVAL 7 DAY))
AND
(`user` = 'username')
GROUP BY
`date`;
MySQL DATE() function reference.
Try this:
SELECT DATE(a.timestamp), COUNT(*)
FROM tableA a
WHERE a.user='username' AND DATEDIFF(NOW(), DATE(a.timestamp)) <= 7
GROUP BY DATE(a.timestamp);
i think it's work :)
SELECT Count(*)
from table A
WHERE user = username AND DATEDIFF(NOW(),timestamp)<=7
I am trying to query a huge database (aprroximately 20 millions records) to get some data. This is the query I am working on right now.
SELECT a.user_id, b.last_name, b.first_name, c.birth_date FROM users a
INNER JOIN users_signup b ON a.user_id a = b.user_id
INNER JOIN users_personal c ON a.user_id a = c.user_id
INNER JOIN
(
SELECT distinct d.a.user_id FROM users_signup d
WHERE d.join_date >= '2013-01-01' and d.join_date < '2014-01-01'
)
AS t ON a.user_id = t.user_id
I have some problems trying to retrieve additional data from the database. I would like to add 2 additional field to the results table:
I am able to get the birth date but I would like to get the age of the members in the results table. The data is stored as 'yyyy-mm-dd' in the users_personal table.
I would like to get the total days since a member joined till the day the left (if any) from a table called user_signup using data from join_date & left_date (format: yyyy-mm-dd).
Or you can do just this ...
SELECT
TIMESTAMPDIFF(YEAR, birthday, CURDATE()) AS age_in_years,
TIMESTAMPDIFF(MONTH, birthday, CURDATE()) AS age_in_month,
TIMESTAMPDIFF(DAY, birthday, CURDATE()) AS age_in_days,
TIMESTAMPDIFF(MINUTE, birthday, NOW()) AS age_in_minutes,
TIMESTAMPDIFF(SECOND, birthday, NOW()) AS age_in_seconds
FROM
table_name
Try this:
SELECT a.user_id, b.last_name, b.first_name, c.birth_date,
FLOOR(DATEDIFF(CURRENT_DATE(), c.birth_date) / 365) age,
DATEDIFF(b.left_date, b.join_date) workDays
FROM users a
INNER JOIN users_signup b ON a.user_id a = b.user_id
INNER JOIN users_personal c ON a.user_id a = c.user_id
WHERE b.join_date >= '2013-01-01' AND b.join_date < '2014-01-01'
GROUP BY a.user_id
You can use datediff function to find number of days between two days like
select datediff(date1,date2) from table
select datediff(curdate(),date2) from table
Getting the current age in years:
SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(DATE(NOW()), birthday)), '%Y') * 1 AS age FROM table_name;
How this works:
datediff(date1, date2) gives the difference between two dates in days. Note that the date format of 'birthday' here is date: YYYY-MM-DD.
from_days converts days into a date format
date_format function extracts with '%Y' only the four digit year. Don't use '%y', because you only get a two digit year and some people are older then 99 years.
multiply the string with 1. This is a 'hack'. MySQL will convert a string like 'YYYY' into an integer.
Getting the current age in month (unlikley, but someone may need this)
SELECT (DATE_FORMAT(FROM_DAYS(DATEDIFF(DATE(NOW()), birthday)), '%Y') * 1 * 12)
+ (DATE_FORMAT(FROM_DAYS(DATEDIFF(DATE(NOW()), birthday)), '%m') * 1) AS age_in_months
FROM table_name;
How this works:
Mostly the same as age in years above.
The years get muliplied by 12. A (earth) year has 12 months.
In the next step the months are extracted the same way as the years, but instead the flag '%Y' must be changed to '%m'.
At the end the two values are added together.
Getting the current age in days is as simple as this:
SELECT DATEDIFF(DATE(NOW()), birthday) AS age_in_days FROM table_name;
Alternative code:
SELECT
DATE_FORMAT(age_date, '%Y') * 1 AS age_in_years,
(DATE_FORMAT(age_date, '%Y') * 1 * 12) + (DATE_FORMAT(age_date, '%m') * 1) AS age_in_months,
age_in_days
FROM
(SELECT
FROM_DAYS(DATEDIFF(DATE(NOW()), birthday)) AS age_date,
DATEDIFF(DATE(NOW()), birthday) AS age_in_days
FROM table_name) AS age_date;