I want to search between two dates in sql - php

I have two tables and this table name is "rooms" and other one is "bookings"
I joined two tables now, I want values when i will search between
book_form = "2016-12-30" and book_to = "2016-12-31"
it will be return true because this two dates does not exists in the "bookings" table, and when search between
book_form = "2016-12-30" and book_to = "2017-01-05"
or book_form = "2017-01-03" and book_to = "2017-01-15"
it will be return false because this date exists in bookings table.
This is my query.
select * from rooms join room_book on rooms.room_id = room_book.room_id
where status = 'available' and room_book.book_from NOT BETWEEN '2016-12-30'
AND room_book.book_to NOT BETWEEN '2016-12-31'

The answer from Kishan Patel returns all records between two dates. So if you want a true or false if a room is available between two dates, you could do something like this:
SELECT IF(COUNT(*) > 0, TRUE, FALSE) AS NewResult
from rooms
join room_book on rooms.room_id = room_book.room_id
where status = 'available'
and room_book.book_from >= '2016-12-30' AND room_book.book_to <= '2016-12-31'

Try this one return all record between two dates
select * from rooms
join room_book on rooms.room_id = room_book.room_id
where status = 'available'
and room_book.book_from >= '2016-12-30' AND room_book.book_to <= '2016-12-31'
make sure its useful to you

Related

mysql select equal data with same date wise

I'm trying to run a mysql join query fetching data from 3 tables.
Table 1: user
Table 2: Outtiming
Table 3: Intiming
I want to show same date data from outtiming and intiming tables with same dates.
e.g: 2017-09-13 = 2017-09-13
But I'm not getting data with same dates.
$query = "SELECT ur.username, ur.user_department, it.*, ot.*
FROM users ur
INNER JOIN intiming it ON ur.staff_id=it.staff_id
INNER JOIN outtiming ot ON ur.staff_id=ot.staff_id
WHERE it.staff_id=".$employee."
AND it.date >= '$startDate' AND ot.date <= '$endDate'";
Result
mysql query result
Add an addition condition in your WHERE statement to check matching dates: it.date = ot.date . Full query:
$query = "SELECT ur.username, ur.user_department, it.*, ot.*
FROM users ur
INNER JOIN intiming it ON ur.staff_id=it.staff_id
INNER JOIN outtiming ot ON ur.staff_id=ot.staff_id
WHERE it.staff_id=".$employee."
AND it.date >= '$startDate' AND ot.date <= '$endDate'
AND it.date = ot.date ";

Searching between two dates in mysql

I have two tables and this table name is "rooms" and other one is "bookings"
I joined two tables now, I want values when i will search between book_form = "2016-12-30" and book_to = "2016-12-31" it will be return true because this two dates does not exists in the "bookings" table, and when search between book_form = "2016-12-30" and book_to = "2017-01-05" or book_form = "2017-01-03" and book_to = "2017-01-15" it will be return false because this date exists in bookings table.
This is my query.
select * from rooms join room_book on rooms.room_id = room_book.room_id where status = 'available' and room_book.book_from NOT BETWEEN '2016-12-30' AND room_book.book_to NOT BETWEEN '2016-12-31'
NOTE: Sorry actually the column book_from date is 2017-01-01 in the bookings table.
select *
from rooms
join room_book on rooms.room_id = room_book.room_id
where status = 'available'
and room_book.book_from >= '2016-12-30'
AND room_book.book_to <= '2016-12-31'
Try like this.
A simple SQL query should return only those rooms without a booking that includes the supplied date:
SELECT *
FROM rooms
LEFT JOIN room_book
ON room_book.room_id = rooms.room_id
AND 'search_date' BETWEEN room_book.book_from AND room_book.book_to
WHERE rooms.room_id IS NULL
AND rooms.status = 'available'
Substitute the date you are searching for search_date above.
By using a LEFT JOIN, you will get all of the records in rooms. The IS NULL test in the where clause eliminates those rows that don't have a matching row in room_book.

How to Intersect multiple tables in mysql

I have set up a php page so there are multiple options for data input that gets put into multiple temporary tables, each one separated with querying the database based on 1 condition given in the data input. So if the input is age > 10 and shoesize > 6 and height > 60, there will be three temporary tables table0, table1, and table2 where table0 is only data age > 10 and table1 is only data shoesize > 6 and table 2 is only data height > 60.
I am wondering how to intersect these so I will only get the results that have all requirements met with age > 10 and shoesize > 6 and height > 60. My attempt using the "WHERE EXISTS" clause is below but it doesn't work.
SELECT *
FROM table0 t0
WHERE EXISTS
(SELECT *
FROM table1 t1
WHERE EXISTS
(SELECT *
FROM table2 t2
WHERE t0.age = t1.age = t2.age
AND t0.shoesize = t1.shoesize = t2.shoesize
AND t0.height = t1.height = t2.height));
Note that queries like this without relying on a table's primary key become cumbersome, so I'd recommend you add a primary key.
That said, the query you need is quite close to what you had already attempted:
SELECT *
FROM table0 t0
WHERE EXISTS (
SELECT 1
FROM table1 t1
WHERE t1.age = t0.age AND t1.gender = t0.gender
AND t1.shoesize = t0.shoesize AND t1.weight = t0.weight
AND t1.height = t0.height AND t1.eyes = t0.eyes) AND
EXISTS (
SELECT 1
FROM table2 t2
WHERE t2.age = t0.age AND t2.gender = t0.gender
AND t2.shoesize = t0.shoesize AND t2.weight = t0.weight
AND t2.height = t0.height AND t2.eyes = t0.eyes)
Note: The query above will work only if none of the values is NULL.

Combining two SQL queries PDO

I'm quite stuck on the following issue. I have a series of tables:
What I want to do is get all the information on a room, assuming that the amount of bookings don't exceed the room number available for that Room.
So to get my Room details my SQL is this:
SELECT Rooms.RoomID as RoomID,
RoomName, NumOfRooms,
MaxPeopleExistingBeds,
MaxExtraBeds,
MaxExtraPeople,
CostPerExtraPerson,
MaximumFreeChildren,
IncludeBreakfast,
MinRate
FROM Rooms, RoomDetails
WHERE Rooms.AccommodationID = :aid AND
Rooms.RoomID = RoomDetails.RoomID
GROUP BY RoomName
Which upon return gets me a list of details for those rooms as follows:
I then use this query to get the number of bookings, and the ID of the room:
SELECT Booking.RoomID,
count(Booking.RoomID) as Bookings
FROM Booking
WHERE ArriveDate >= :aDate AND
DepartDate <= :dDate AND
AccommodationID = :aid
GROUP BY RoomID
I then combine both and feed the two arrays back in one array using this function:
public function get_availability($aid, $aDate, $dDate) {
$stmt = $this->db->prepare('SELECT Rooms.RoomID as RoomID, RoomName, NumOfRooms, MaxPeopleExistingBeds, MaxExtraBeds, MaxExtraPeople, CostPerExtraPerson, MaximumFreeChildren, IncludeBreakfast, MinRate FROM Rooms, RoomDetails WHERE Rooms.AccommodationID = :aid AND Rooms.RoomID = RoomDetails.RoomID GROUP BY RoomName');
$stmt->bindValue(':aid', $aid);
$stmt->execute();
$rooms = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt2 = $this->db->prepare('SELECT Booking.RoomID, count(Booking.RoomID) as Bookings FROM Booking WHERE ArriveDate >= :aDate AND DepartDate <= :dDate AND AccommodationID = :aid GROUP BY RoomID');
$stmt2->bindValue(':aid', $aid);
$stmt2->bindValue(':aDate', $aDate);
$stmt2->bindValue(':dDate', $dDate);
$stmt2->execute();
$bookings = $stmt2->fetchAll(PDO::FETCH_ASSOC);
$room = array($rooms, $bookings);
return (!empty($room)) ? $room : false;
}
The thing is, what I actually want to do is only return the room details where NumOfRooms is less than the number of Bookings.
So for instance where I have $bookings, if it tells me that for room ID 4, I have 3 bookings for a set period, and my NumOfRooms is 1. Then I know that I have no capacity that week to take any more bookings on. If however I have 1 booking and one capacity then that is still full. But if I have NumOfRooms of 2, and bookings amount to 1, I know I have room.
So basically if NumOfRooms > BookingCount then the room is available.
How can I amalgamate both queries and simplify my code to make this possible?
I.E to put it simply, how do I select all of the info from RoomDetails given an ArriveDate in Booking and a DepartDate and a RoomID, where NumOfRooms > count(Booking.RoomID) (Where it is within those dates and the room id is equal to the room id of Rooms).
Your problem can be solved by simply updating the SQL statement itself:
SELECT r.RoomID AS RoomID,
RoomName,
NumOfRooms,
MaxPeopleExistingBeds,
MaxExtraBeds,
MaxExtraPeople,
CostPerExtraPerson,
MaximumFreeChildren,
IncludeBreakfast,
MinRate
FROM Rooms r
JOIN RoomDetails rd
ON r.RoomID = rd.RoomID
JOIN (
SELECT b.RoomID,
AccommodationID,
count(b.RoomID) AS Bookings
FROM Booking b
WHERE ArriveDate >= :aDate
AND DepartDate <= :dDate
GROUP BY RoomID
) t
ON t.AccommodationID = r.AccommodationID
WHERE r.AccommodationID = :aid
AND t.Bookings < NumOfRooms
GROUP BY RoomName
You can select out all of the booking counts per room for the desired date range as a subquery, and then LEFT JOIN that subquery against the list of your rooms filtered by your desired AccommodationID and the desired NumOfRooms > BookingCount criteria. The key here is in the join type used for this subquery, as an inner join would limit your results to only rooms that actually had bookings.
SELECT Rooms.RoomID as RoomID,
RoomName, NumOfRooms,
MaxPeopleExistingBeds,
MaxExtraBeds,
MaxExtraPeople,
CostPerExtraPerson,
MaximumFreeChildren,
IncludeBreakfast,
MinRate,
BookingCount
FROM Rooms
INNER JOIN RoomDetails on Rooms.RoomID = RoomDetails.RoomID
LEFT JOIN (
SELECT Booking.RoomID,
count(Booking.RoomID) as BookingCount
FROM Booking
WHERE ArriveDate >= :aDate AND
DepartDate <= :dDate
GROUP BY Booking.RoomID
) RoomBookings ON Rooms.RoomID = RoomBookings.RoomID
WHERE Rooms.AccommodationID = :aid
AND NumOfRooms > BookingCount
GROUP BY RoomName

PHP select timediff with select query and group by user

i have this SQL Query:
$sql="SELECT *, COUNT(assigned_to) AS my_groupcount from tickets where deleted = '' and DAY(datetime) = '".$_GET["d"]."' and MONTH(datetime) = '".$_GET["m"]."' and YEAR(datetime) = '".$_GET["Y"]."' group by assigned_to order by datetime ASC ";
$rs=mysql_query($sql,$conn);
while($result=mysql_fetch_array($rs))
{
//work out the total time taken
$sql3="SELECT *, TIMEDIFF( timeend, timestart ) AS support_time_used FROM ticket_updates WHERE ticket_seq = '".$result["ticketnumber"]."' ";
$rs3=mysql_query($sql3,$conn) or die (mysql_error());
$totaltime = 0;
while($result3=mysql_fetch_array($rs3))
{
$totaltime = $totaltime+substr($result3["support_time_used"],0,2)*60+substr($result3["support_time_used"],3,2);
}
$hours=intval($totaltime / 60);
$minutes=$totaltime -($hours * 60);
$total_ticket_time = $hours.'h '.$minutes.'m';
echo $result["assigned_to"].' ('.$result["my_groupcount"].') - Time: '.$total_ticket_time.'<br>';
}
so its selecting all the rows from the tickets table and grouping by the asigned_to column.
it then works out the time used for each user from the ticket_updates table.
the ticket_updates.ticket_seq column links to the tickets.ticketnumber column and there may be just one or multiple rows in the ticket_updates table for 1 row in the tickets table so it adds all the differences up in the ticket_updates table.
im trying to list the assigned_to (one each) from the tickets table and put next to each one how much time they have used but its only selecting it form one ticket.
how can i make it select from all the tickets?
you don't need to do it in two steps, do you.
select assigned_to, sum(TIMEDIFF( timeend, timestart )) as total_time, count(distinct ticket_id) as ticket_count from ticket_update where ticket_id in (select ticket_id from ticket where condition)
group by assigned_to
edit: you actually need:
select ticket.assigned_to,
sum(TIMEDIFF( ticket_update_timeend, ticket_update.timestart )) as total_time, count(distinct ticket.ticket_id) as ticket_count
from
ticket_update
inner join ticket
on ticket.ticket_id=ticket_update.ticket_id
where ticket.deleted = '' and DAY(ticket.datetime) = '".$_GET["d"]."' and MONTH(ticket.datetime) = '".$_GET["m"]."' and YEAR(ticket.datetime) = '".$_GET["Y"]."'
group by ticket.assigned_to

Categories