This question already has answers here:
Booking Calendar Arrive & Depart Dates
(2 answers)
Closed 4 years ago.
Hi guys i'm creating a simple web application in Java in which i should book a room. Now my problem concerns the possibility of booking a room in the same period.
Let me explain better, I have 2 types of rooms, bedroom1 and bedroom2. If user1 has booked room1 on 22/08/2018, user2 can not book room1 on the same date.
Since I use a database I thought I could solve it by query.
In particular my database:
Table Name: reservation
id_book,login,email,typeroom,numroom,arrivaldate,departuredate.
I have tried to use this query:
SELECT res1.id_prenotazione, res1.typeroom, res1.arrivaldate, res1.departuredate
FROM reservation res1, reservation res2
WHERE ( res1.typeroom = res2.typeroom ) AND (res1.arrivaldate = res2.arrivaldate )
But I don't resolve the problem.
Can you help me??
UPDATE.
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// out.println("driver loaded");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Hotel?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root" ,"123456789");
out.println("Connect");
Statement st = con.createStatement();
Statement stmt = con.createStatement();
out.println("connection successfull");
String check = ("SELECT res1.id_prenotazione, res1.typeroom, res1.arrivaldate, res1.departuredate\n" +
"FROM reservation res1, reservation res2\n" +
"WHERE res1.numroom = res2.numroom\n" +
"AND ((res1.arrivaldate <= res2.departuredate AND res1.departuredate >= res2.arrivaldate)\n" +
" OR (res2.arrivaldate <= res1.departuredate AND res2.departuredate >= res1.arrivaldate))");
ResultSet rs2 = stmt.executeQuery(check);
int rs = st.executeUpdate("insert into reservation (login,email,typeroom,numroom,arrivaldate,departuredate)values ('"+login+"','"+email+"','"+typeroom+"','"+numroom+"','"+arrivaldate+"','"+departuredate+"')");
I'm trying first to use the database version of your answer. I have write this, to check if the booking is ok or not i should insert an if controller?
I think you mean overlap of reservations for the same room:
SELECT res1.id_prenotazione, res1.typeroom, res1.arrivaldate, res1.departuredate
FROM reservation res1, reservation res2
WHERE res1.room_no = res2.room_no
AND res1.id_prenotazione != res2.id_prenotazione
AND res1.arrivaldate <= res2.departuredate AND res1.departuredata >= res2.arrivaldate
What should maybe checked too is: you used date equality. If that failed, maybe your table columns are not DATE but DATETIME/TIMESTAMP. That would be understandable
as someone might leave his room at 9:00 and the next enter at say 12:00.
AND (res1.arrivaldate = res2.arrivaldate ) would be wrong. You need a range
e.g AND (res1.arrivaldate > res2.arrivaldate ) AND (res1.arrivaldate < res2.departuredate) or something similar
If you want overlapping periods:
SELECT res1.id_prenotazione, res1.typeroom, res1.arrivaldate, res1.departuredate
FROM reservation res1, reservation res2
WHERE ( res1.typeroom = res2.typeroom )
AND res1.id_book != res2.id_book
AND (res1.arrivaldate <= res2.departuredate)
AND (res2.arrivaldate <= res1.departuredate)
Related
Good day,
I have a question.
I have a table called event_booking.
Here we have events and each event has a separate location.
So we have event
event_booking has :
booking_id
booking_name
booking_date
booking_location
booking_comments
The type of structure is :
booking_id = integer
booking_name = varchar
booking_date = datetime
booking_location = integer
booking_comments = text
Now I want to create a Query that lists all event locations and for each location which event has been set there.
So :
I would get a result like :
booking_location = 4 & no_bookings = 256
booking_location = 7 & no_bookings = 34
booking_location = 6 & no_bookings = 128
booking_location = 3 & no_bookings = 24
Now I have fiddled a lit and created the following QUERY.
That's correct in terms of syntax. But totally wrong in terms of output.
SELECT `booking_location`, `booking_location` AS `selector`, (SELECT COUNT(1) FROM `event_booking` WHERE `booking_location` = `selector`) AS `no_bookings` FROM `event_booking` WHERE `booking_location` IN (SELECT `booking_location` FROM `event_booking` GROUP BY `booking_location`)
I know I am missing something. But what I am missing..
Thanks in advance ;-)
Assuming that you really just want to count the booking locations (based on your example of a possible result, and also your query code):
SELECT booking_location,
COUNT(booking_location) AS no_bookings
FROM event_booking
GROUP BY booking_location;
This question already has answers here:
How to convert a string to date in MySQL?
(5 answers)
Closed 2 years ago.
I am trying to compare the following:
SELECT g_m.user_id
, g_m.group_id
FROM Group_Members g_m
WHERE g_m.gender_id = 2
AND g_m.partner_gender_id = 1
AND g_m.birthday >= '01-01-1955'
AND g_m.birthday <= '12-31-2002'
AND g_m.user_id != 12
g_m.birthday in this case is '02-15-1998' which should show up, but this returns an empty array, because the date comparison does not seem to be accurate?
Here is the entire function and the dates are being passed from age minimum and age maximums brought from user.
var today = new Date();
var minYear = "01-01-" + (today.getFullYear() - userPref.age_max); //min year to start but oldest age
var maxYear = "12-31-" + (today.getFullYear() - userPref.age_min); //max year to end but youngest age
var qSelect = "SELECT g_m.user_id, g_m.group_id" +
" FROM Group_Members g_m WHERE g_m.gender_id = ? AND g_m.partner_gender_id = ?" +
" AND g_m.birthday >= STR_TO_DATE(?, '%m/%d/%Y') AND g_m.birthday <= STR_TO_DATE(?, '%m/%d/%Y')" +
" AND g_m.user_id != ?";
var qValues = [userPref.partner_gender_id, userObj.gender_id, minYear, maxYear, userObj.id];
Anyone know how to compare dates in a mysql query?
What is the data type of your dates ? You should declare them as "date", otherwise you won't be able to compare them.
With strings, '02-15-1998' < '03-15-1990'
With dates, your mysql request should be :
SELECT g_m.user_id, g_m.group_id FROM Group_Members g_m WHERE g_m.gender_id = 2 AND g_m.partner_gender_id = 1 AND g_m.birthday >= '1955-01-01' AND g_m.birthday <= '2002-12-31' AND g_m.user_id != 12
Sorry for my english, I'm french...
As the comments have already pointed out, you appear to be storing your dates as actual text. For a short term workaround, you may use STR_TO_DATE to convert your text dates to bona fide MySQL dates. Then, compare them against valid MySQL date literals:
SELECT
g_m.user_id,
g_m.group_id
FROM Group_Members g_m
WHERE
g_m.gender_id = 2 AND
g_m.partner_gender_id = 1 AND
STR_TO_DATE(g_m.birthday, '%m-%d-%Y') >= '1955-01-01' AND
STR_TO_DATE(g_m.birthday, '%m-%d-%Y') < '2003-01-01' AND
g_m.user_id != 12;
Longer term, you should make the birthday column datetime or timestamp.
Side note: I have rewritten the date range to include those born in the calendar years from 1955 to 2002, inclusive on both ends.
I am trying to get all rows from my MySQL database that are from the last week and also have the right company_id. I don't understand why this isn't returning any data, as there are no errors.
A screenshot of the database:
My code:
// Create Calendar and Date objects
Calendar calendar = Calendar.getInstance();
java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());
// Get all datapoints from last week from this company's id
query = "SELECT * FROM survey_data WHERE (company_id = ?) AND (date_entered BETWEEN ? + interval ? day AND ? + interval ? day)";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setString(1, companyId);
preparedStatement.setDate(2, date);
preparedStatement.setInt(3, -Calendar.DAY_OF_WEEK - 6);
preparedStatement.setDate(4, date);
preparedStatement.setInt(5, -Calendar.DAY_OF_WEEK);
resultSet = preparedStatement.executeQuery();
I have a table that contains events data. I also have a list of dates that I need to check through. So I loop though the given dates and query my table. I need to select the events that we on a given date and if the date is the same as today, I only need events that took place before certain time. The time is stored in a military format, like 14:30.
I think I've overcomplicated my query logic:
$current_date = date('Y-m-d');
$current_time = date('H:i');
The reason I use PHP time and date, because I can have an accurate time from a server I control. MySQL time is off by a few hours...
SELECT *
FROM events
WHERE org.id = ".$ID."
AND (
(
DATE(events.date) = '".$givenDate."'
AND
'".$givenDate."' < '".$current_date."'
)
OR
(
DATE(events.date) = '".$givenDate."'
AND
'".$givenDate."' < '".$current_date."'
AND
events.time < '".$current_time."'
)
)
SELECT *
FROM events
WHERE org.id = ".$ID."
AND date(events.date) = '".$givenDate."'
and if ('".$givenDate."' = '".$current_date."',
events.time < '".$current_time."', true)
I have a transport planner written in PHP and MySQL,
To get the task rules per day, I use the following query:
SELECT planning.*,
planning_dagen.planning_id,
planning_dagen.dagen,
planning_dagen.data_import,
routenummer_wijzigingen.routenummer AS temp_routenummer
FROM planning
LEFT JOIN planning_dagen
USING (planning_id)
LEFT JOIN routenummer_wijzigingen
USING (planning_id)
WHERE :datum >= planning.datum
AND :datum <= geldig_tot
AND (frequentie = 'dagelijks' AND dayofweek(:datum) = planning_dagen.dagen
OR (frequentie = 'eenmalig' AND date(:datum) = planning.datum)
OR (frequentie = 'wekelijks' AND 0 = (abs(datediff(:datum, planning.datum)) % 7))
OR (frequentie = 'twee-wekelijks' AND 0 = (abs(datediff(:datum, planning.datum)) % 14))
OR (frequentie = 'maandelijks'
AND ceil(dayofmonth(:datum)/7) = ceil(dayofmonth(planning.datum)/7)
AND dayofweek(:datum) = dayofweek(planning.datum)))
AND dayofweek(:datum) <> '1'
AND dayofweek(:datum) <> '7'
In the planning table there is a column called routenummer (routenumber) which is used in most conditions (standard routenumber).
But as you can see I have also a routenummer_wijzigingen table which is used to give a task a different routenumber for certain day.
For example I have a task which returns every tuesday and wednesday and has routenumber 10. But on tuesday 2015-02-03 I need this task done by routenumber 9.
So I insert a rule in the routenummer_wijzigingen table which has the following columns:
routenummer_wijzigingen_id
planning_id
routenummer
datum
So when a date is selected and that date and planning_id exists in the routenummer_wijzigingen table, it has to take the routenumber from the routenummer_wijzigingen table instead of the planning table.
How can I achieve this?
You should modify join condition with routenummer_wijzigingen table (including datum). Then you should use CASE in your SELECT clause to decide which routenummer to choose.
SELECT planning.*,
planning_dagen.planning_id,
planning_dagen.dagen,
planning_dagen.data_import,
CASE
WHEN routenummer_wijzigingen.routenummer is not NULL
THEN routenummer_wijzigingen.routenummer
ELSE planning.routenummer
END AS temp_routenummer
FROM planning
...
LEFT JOIN routenummer_wijzigingen rw on
planning.planning_id=rw.planning_id and rw.datum=...