Please note that this is not my full code for the query. I just need to understand how to compare the time only. Here is my table creation code
CREATE TABLE IF NOT EXISTS `mydb`.`ActivityBooking` (
`ActivityTime` datetime NOT NULL,
`NumPeople` INT NULL,
`ActivityID` VARCHAR(45) NOT NULL,
`GuideID` VARCHAR(10) NOT NULL,
`Reservation_ReservationID` VARCHAR(10) NOT NULL,
PRIMARY KEY (`ActivityTime`, `ActivityID`),
INDEX `fk_ActivityBooking_Activity1_idx` (`ActivityID` ASC) VISIBLE,
INDEX `fk_ActivityBooking_Staff1_idx` (`GuideID` ASC) VISIBLE,
INDEX `fk_ActivityBooking_Reservation1_idx` (`Reservation_ReservationID` ASC) VISIBLE,
CONSTRAINT `fk_ActivityBooking_Activity1`
FOREIGN KEY (`ActivityID`)
REFERENCES `mydb`.`Activity` (`ActivityID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_ActivityBooking_Staff1`
FOREIGN KEY (`GuideID`)
REFERENCES `mydb`.`Staff` (`StaffID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_ActivityBooking_Reservation1`
FOREIGN KEY (`Reservation_ReservationID`)
REFERENCES `mydb`.`Reservation` (`ReservationID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
Table insertion
Insert into activitybooking values('2020-10-7 15:17:00',6,'C01','S5','R321');
Insert into activitybooking values('2020-12-8 16:15:00',7,'D01','S2','R321');
Insert into activitybooking values('2020-11-9 18:12:00',2,'E01','R321','S4');
Currently I am stuck because I want to display results for bookings made after mid-day and before 4pm. But I don't know how to just compare the time from the datetime format. I am not adding the codes for the other tables in order to keep the information here minimum. But if needed I can provide.
select customerfname, customerlname, activityname
from customer, activitybooking,activity
where activity.activityid = activitybooking.activityid
and activitytime between time('12-0-0') and time('16-0-0');
What you need to do is apply the TIME
function to your activitytime
column and then compare it to 12:00:00
and 16:00:00
:
TIME(activitytime) BETWEEN '12:00:00' AND '16:00:00'
Note if you don't want to include 16:00:00
replace that with 15:59:59
. Also, if you write the time strings in HH:mm:ss
format then you don't need to apply TIME
to them and can just compare directly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.