I got a table called calendar, in that table I have this rows:
-day
-month
-year
why? Because I need it hehe.
So, the problem is when I want search a date in diferent year ( in the same year it's ok), for example:
Days between: 31-12-2013 and 1-1-2014, so I have a query:
SELECT * FROM calendar WHERE concat(year,'-',month,'-',day) BETWEEN '2013-1-30' AND '2013-1-31';
But, this query search in the same year and show the another months (I have in my db the 730 days) and not the 2014.
So, I'm confused because, how can I concat the rows and search with between?
Hope you can help me to understand, and sorry for my english.
Thanks for all.!!
Use str_to_date:
SELECT * FROM calendar
WHERE str_to_date(concat(year,'-',month,'-',day),'%Y-%m-%d')
BETWEEN '2013-1-30' AND '2013-1-31';
sqlfiddle demo
Try
SELECT * FROM calendar WHERE DATE(CONCAT(year,'-',month,'-',day)) BETWEEN '2013-1-30' AND '2013-1-31';
I'm also curious as to why you 'need' to store year, month and day as three seperate fields, and not in a single date field?
I think year, month and day may also be reserved keywords in mysql, so you may have to do
SELECT * FROM `calendar` WHERE DATE(CONCAT(`year`,'-',`month`,'-',`day`)) BETWEEN '2013-1-30' AND '2013-1-31';
Related
I'm working whit a MariaDB database.
I need to know, for every day in a certain time the avg of a count.
I'd tried somethink like this.
SELECT AVG(dayShipments), weekdays
FROM (SELECT COUNT(idShipment) as "dayShipments", WEEKDAY(dateShipments) as "weekdays"
FROM weekdays
WHERE dateShipments BETWEEN '2021-05-01'AND '2021-05-21'
GROUP BY dateShipments) as t1
GROUP BY weekdays
My boss told me that this query ignore the day where I don't have any Shipment.
How can i inlude that?
Sorry for my bad English and thanks for helping me
If you want to summary by day-of-the-week (which is what your query appears to be doing. And you want to treat days with no shipments as 0, then use SUM() and division:
SELECT WEEKDAY(dateShipments) as weekday,
COUNT(*) / 3 as dayShipments as avg_per_day
FROM weekdays
WHERE dateShipments BETWEEN '2021-05-01'AND '2021-05-21'
GROUP BY weekday;
The 3 is because the query spans three weeks.
First of all don't worry about your english. It's good enough.
Secondly, your boss is right. If you have no records in "weekdays" table for specific day, the mentioned day will never show up with this query.
For solving problem I think you need to have a temporary table for day of week and left join with your t1 table.
this is my first question here, thanks for your support.
I have an old database with many columns, but the is not a DATE column. Yes, my mistake.
But, I have a column called question_id with some data, that contains the date and time, like
2017113020440370769
where first 4 digits are YYYY, next 2 digits are MM and next 2 are DD.
I´m trying to make a query extracting those characters to count how many questions I had on specific dates.
I could, doing this for a specific date:
SELECT COUNT(*)
FROM questions
WHERE question_id LIKE '20171228%';
But I want to automatize this for TODAY, everyday, not typing each day the YYYYMMDD%
Is it possible? Using CURDATE? Or there is any specific TIMESTAMP I can use?
Thank you very much!
Yes it is possible, you can build the current dates with wild card as below:
SELECT COUNT(*)
FROM questions
WHERE question_id LIKE CONCAT(DATE_FORMAT(CURDATE(), '%Y%m%d'), '%');
That's for a specific day; today. For all dates you would have to do an aggregation on the date part of the question_id as illustrated below:
SELECT SUBSTR(question_id,1,8) ASKED_ON, COUNT(*) NUMBER_OF_QUESTIONS
FROM questions
GROUP BY SUBSTR(question_id,1,8);
You can get today's date with e.g. CURDATE()
You can format dates with DATE_FORMAT()
You con concatenate strings with CONCAT()
Those are all the bricks you need ;-)
Try this:
SELECT LEFT('20171228ASD',8) = CURRENT_DATE();
SELECT COUNT(*)
FROM questions
WHERE LEFT(question_id',8) = CURRENT_DATE();
I'm trying to find the cumulative sum of sessions of a link for its first 3 days. I tried this but it doesn't seem to take the date clause into account:
select
date,
link,
sum(sessions) as sessions
from ga
where date <= date+interval 3 day
group by link
But if I manually enter a date, it seems to work. Why is it not seeing date+interval 3 day as a proper date...?
Any help would be greatly appreciated! :)
Date is a column, not a value, you need to provide a specific date entry. Also "between" is a better keyword to use in this situation.
You need to also add date column in GROUP BY clause. Also, avoid using column name as date. It will create confusion.
Try below query :
select date_column,
link,
sum(sessions) as sessions
from ga
where date_column BETWEEN CURDATE()-3 AND CURDATE()
group by link, date_column
I have timestamp values in my db. It has values like 2014-11-25 10:30:00.
I need to get all records between two dates and that has time between certain range like between 2014-10-20 to 2014-11-25 and between 9am to 7pm..
I need the query for this...
You can use the following query , I used it in my code for displaying data between two dates.
SELECT * from tablename WHERE columnname BETWEEN '2014-10-20 00:00:00' AND '2014-11-25 23:59:59'
The query includes start time of the particular date to end time of ending particular date.
You edit your query according to your start and end timings.
You can use internal mysql functions for convert datetype.
I think you need DATE() and TIME() functions.
Details you can find here
Thanks for your reply guys. I have found the answer
SELECT * FROM alerts
WHERE DATE BETWEEN '2014-11-16' AND '2014-11-26'
AND TIME(DATE) BETWEEN '09:00' AND '19:00'
Is giving the expected result.. :-)
In my database, there is a field called birthday(date) and I want to retrieve records that their birth month and birth day is equal to the current month and the day. Is there a way to write a query for this? Or I just have to do it by retrieving all of the records and find the matching records after by using another program? Thanks!
This might also work: (NOTE that I am not sure if you mean day within a Month or day within a week)?
SELECT * FROM TABLE
WHERE MONTH(birthday) = MONTH(CURRENT_DATE)
AND DAY(birthday) = DAY(CURRENT_DATE) --assuming day within a month
select * from table
where date_format(birthday, '%m%d')=date_format(current_date, '%m%d');
The above query would not make use of mysql index