Identifying dates from datespan for multiple entries - mysql

I need your guidance in regard to a query I am trying to write, but hitting a wall on. I am working on a project for a scheduling system. The scheduling system has in it a dates calendar table, a yearsemester table with datespans, a courses table with courses assigned to yearsemesters, and a components table with the course components (days of week & times) listed.
I need to write a query that identifies every day of the week within a given datesspan (parameters), from a web form, where a class takes place.
These tables approximately look like the example below:
(Dates) Calendar Table:
Date
DayofWeek
2021-01-01
Friday
2021-01-02
Saturday
2021-01-03
Sunday
.... On and on for like 20 years worth of dates
(YearSemester) table:
ID
Year
Semster
Start Date
End Date
1
2021
Spring
2021-01-04
2021-05-01
2
2021
Summer
2021-06-01
2021-08-15
3
2021
Fall
2021-08-15
2021-12-30
(CoursesSchedule) table:
ID
YearSemesterID
CourseID
1
1
CTI120
2
1
DBA110
3
1
DBA225
(ComponentSchedule) table:
ID
CourseScheduleID
Component
DayofWeek
Start Time
End Time
1
1
Class
Monday
8:00 AM
10:00 AM
2
1
lab
Monday
11:00 AM
11:50 AM
3
1
Class
Wednesday
8:00 AM
10:00 AM
4
1
Lab
Wednesday
11:00 AM
11:50 AM
5
2
Class
Tuesday
5:00 PM
9:00 PM
6
2
Class
Thursday
5:00 PM
9:00 PM
7
3
Class
Monday
1:00 PM
3:00 PM
8
3
Class
Friday
1:00 PM
3:00 PM
I need to find a way using either the calendars table, or some other method, to generate a result set like the following when given start and end dates as parameters from a web form
INTENDED QUERY OUTPUT
YEAR
SEMESTER
COURSE
COMPONENT
DATE
Start Time
End Time
2021
SPRING
CTI110
CLASS
2021-01-04
8:00 AM
10:00 AM
2021
SPRING
CTI110
LAB
2021-01-04
8:00 AM
10:00 AM
2021
SPRING
DBA225
CLASS
2021-01-04
1:00 PM
3:00 PM
2021
SPRING
DBA110
CLASS
2021-01-05
5:00 PM
9:00 PM
2021
SPRING
CTI110
CLASS
2021-01-06
8:00 AM
10:00 AM
2021
SPRING
CTI110
LAB
2021-01-06
8:00 AM
10:00 AM
... And so on and so forth to the end of the date span. This is to be displayed on the webpage, and used in a calendar view eventually, with some formatting of course.
I can't figure out how to generate the individual dates of occurrence by referencing the semester's date span against the course's component days of the week. Should this be a loop?
Any advice, or guidance, if you have a good guide on a topic such as this, would be greatly appreciated!! I haven't been able to find anything super helpful so far.

Related

SQL Get Data Between Two dates and Time specifically then break and go to next Date with same Time

Hello guys hope you're having a nice day.
What I'm trying to do is to get data from my db monthly. But what I want is to get data every day for example of all January:
DECLARE
#FromDate Datetime = Convert(datetime,'2022-01-01 12:00:01'),-- all January
#Datetime = Convert(datetime,'2022-01-31 17:00:00')
I have a table called Delivery_order_dtm which has the date and the time of item delivery.
I want every day the data between 12 :00:01 and 17:00:00 ONLY. so when the program finishes fetching data of 1-1-2022 between 12:00:01 and 17:00:00 I want it to go to 02-02-2022 and do the same thing between 12:00:01 and 17:00:00 then go to 03-03-2022 and so on.
Right now how I wrote it it is giving me all the data starting from 01-01-2022 12 PM and ending in 31-01-2022 5 PM that is not what I want.
I want everyday after fetching from 12 pm to 5 pm of this day to break and go to the next day from 12 pm to 5 pm.
Hope you can help me thank you!

Configure columns when importing CSV into MySQL

I'm using phpmyadmin to import a CSV file into a MySQL database, the file contains a column that has 672 binary numbers, if I import that without checking the box The first line of the file contains the table column names (if this is unchecked, the first line will become part of the data) everything goes in as VARCHAR columns and life is good.
If I check that box however, it tries to insert that column as a BIGINT and it fails because the maximum value for that is 255.
How can I modify the fields in the CSV file or while importing to MySQL to say consider every column is a varchar?
EDIT
Here's a subset of the data I'm trying to import. Save that into a CSV file:
ZomatoURL,PhoneNumber,More Info,Claimed,Booking,Hours2,RestaurantName,HoursMon,HoursTue,HoursWed,HoursThu,HoursFri,HoursSat,HoursSun
https://www.zomato.com/sydney/koi-dessert-bar-chippendale,02 9212 1230,More Info Takeaway Available Wheelchair Accessible No Alcohol Available Gluten Free Options Desserts and Bakes Kid Friendly Vegetarian Friendly,Report Error Claim this listing,,MonClosedTue10 AM to 10 PMWed10 AM to 10 PMThu10 AM to 10 PMFri10 AM to 10 PMSat10 AM to 10 PMSun10 AM to 10 PM,KOI Dessert Bar,000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,000000000000000000000000000000000000000111111111111111111111111111111111111111111111111000000000,000000000000000000000000000000000000000111111111111111111111111111111111111111111111111000000000,000000000000000000000000000000000000000111111111111111111111111111111111111111111111111000000000,000000000000000000000000000000000000000111111111111111111111111111111111111111111111111000000000,000000000000000000000000000000000000000111111111111111111111111111111111111111111111111000000000,000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111000
https://www.zomato.com/sydney/kobe-wagyu-yakiniku-cbd,02 9283 2268,More Info No Alcohol Available Sake Buffet,Claimed listing?,Table booking recommended,"Mon 12 Noon to 2:30 PM, 5:30 PM to 10:30 PM Tue 12 Noon to 2:30 PM, 5:30 PM to 10:30 PM Wed 12 Noon to 2:30 PM, 5:30 PM to 10:30 PM Thu 12 Noon to 2:30 PM, 5:30 PM to 10:30 PM Fri 12 Noon to 2:30 PM, 5 PM to 11:30 PM Sat 12 Noon to 11:30 PM Sun 12 Noon to 10:30 PM",Kobe Wagyu Yakiniku,000000000000000000000000000000000000000000000000111111111100000000000011111111111111111111000000,000000000000000000000000000000000000000000000001111111111000000000000111111111111111111110000000,000000000000000000000000000000000000000000000001111111111000000000000111111111111111111110000000,000000000000000000000000000000000000000000000001111111111000000000000111111111111111111110000000,000000000000000000000000000000000000000000000001111111111000000000011111111111111111111111111000,000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111000,111000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111110
https://www.zomato.com/sydney/home-thai-cbd,02 9261 5058,More Info Takeaway Available BYO BYOW Vegetarian Friendly,Report Error Claim this listing,,Mon 11 AM to 11 PM Tue 11 AM to 11 PM Wed 11 AM to 11 PM Thu 11 AM to 11 PM Fri 11 AM to 11 PM Sat 11 AM to 11 PM Sun 11 AM to 11 PM,Home Thai,000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111110000,000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111100000,000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111100000,000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111100000,000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111100000,000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111100000,100000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111

MySQL Custom WEEK() Mode

I am trying to create a weekly report (including grouping by WEEK(date)) where weeks start on Monday. However, Week 1 is always the first week any day of a new year occurs. For example if Jan 1st is a Friday, that is week one.
This doesn't seem to be consistent with MySQL mode options for WEEK():
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
Based on the available options it only works if the week has 4 or more days (options 1 and 3) or the Monday is in the new year (options 5 and 7). Neither of these account for example week 1 of 2016:
Wk Mon Tue Wed Thu Fri Sat Sun
No.
1 28 29 30 31 1 2 3
Is there any way to order/number the week numbers in MySQL based on this custom week mode requirement?

mysql query to exclude sunday from two dates

If complaint filed on 5 pm on Saturday and closed on 1 pm on Monday, ticket should exclude sunday 24 hours.
Complaint Creation Date:24 Sept 2014, 6:00 PM Sat
Complaint Closed Date : 26 Sept 2014 ,6:00 PM Monday
Time taken :1 day not 2 day
Need Suggestions..
You can try somthing like this:-
SELECT CASE WHEN DAYNAME(DATE_ADD(Complaint_Creation_Date, INTERVAL 1 day)) = 'Sunday'
THEN (TIMESTAMPDIFF(HOUR,Complaint_Creation_Date,Complaint_Closed_Date))/24 - 1 AS "TIME TAKEN"
ELSE
(TIMESTAMPDIFF(HOUR,Complaint_Creation_Date,Complaint_Closed_Date))/24 AS "TIME TAKEN"
END
FROM YOUR_TABLE;

Compare current time with a time in a row using SQL in MySql

first of all i have a table like this:
userID Day Hour Class
---------------------------------------
65 Monday 08:00 Math
65 Monday 09:00 Bio
65 Monday 13:00 History
65 Tuesday 08:00 Sports
65 Friday 10:00 Math
I am choosing the classes according to the current time.
When it is 08:30 then i need to choose the next Hour which is 09:00 and it is Bio. And it is fine.
But when it is 10:00 then i need to choose 13:00 which is History.
Added: lets say today is Tuesday and it is 07:00 o'clock. With the code below , I can choose tne next coming Class whish is at 08:00 on Tuesday.
Lets assume today is Tuesday and 12:00 o'clock. How can I choose the next lecture which is on Friday (next weekday that I have a class on).
When I have no class in one day ,how can I choose the next avaible day that I have a class ...?
try something like this:
select `Hour`, Class
from your_table
where time(`Hour`) > curtime()
and `Day` = DAYNAME(now())
order by time(`Hour`) asc
limit 1