Counting field total against another table column value - php

room | beds available | ****table room****
==================================
room1 | 4
room2 | 2
room3 | 4
room | occupant | ****table occupant****
==================================
room1 | arnold
room1 | berry
room2 | charles
room2 | daisy
room3 | eric
room3 | frank
room3 | greg
I looking to get the following output:
No. of rooms with beds available: 2 || Rooms with beds available: room 1, room 3
i reckon i need to
store count* for each unique room and store the count as an array
subtract beds available for each room against this array
display the room name when there's a result > 0
How should the php snippet code look like?

These SQL queries will do the trick.
Remember, you can remove unwanted columns to save the amount of data you're processing...
select r.id AS 'room',
r.beds as 'total_beds',
count(o.occupant) as 'taken_beds',
r.beds-count(o.occupant) as 'free_beds'
FROM room r LEFT JOIN occupant o ON r.id = o.room
GROUP BY r.id
HAVING r.beds > count(o.occupant)
This will only return the rooms that are not full.
If at any other point, you wish to return full rooms too, simply remove the "HAVING" clause
select r.id AS 'room',
r.beds as 'total_beds',
count(o.occupant) as 'taken_beds',
r.beds-count(o.occupant) as 'free_beds'
FROM room r LEFT JOIN occupant o ON r.id = o.room
GROUP BY r.id

I think this all can be done with a simple SQL query that should look something like this:
SELECT room.beds AS beds, COUNT(occupant.occupant) AS beds_occupied
FROM room
LEFT JOIN occupant ON occupant.room = room.room
GROUP BY room
HAVING (beds - beds_occupied) > 0;
You can use the 'HAVING' clause which is similar to the WHERE clause but works with aggregates. I haven't run this exact query on your exact tables so there might be a typo, however, I hope the idea what the query is supposed to do is clear.

So something like this where you can change how many available beds you need..
SELECT r.*, COUNT(*) AS occupied, r.beds - COUNT(*) AS rest
FROM room r
LEFT JOIN occupant o ON r.room = o.room
GROUP BY r.room
HAVING r.beds - occupied >= 1

MySQL offers GROUP_CONCAT to aggregate strings:
select
count(*) as number_of_rooms,
group_concat(room) as rooms
from room r
where beds_available >
(
select count(*)
from occupant o
where o.room = r.room
);
This selects rooms with more beds available than occupied and then aggregates the resulting rows to one row containing the number of available rooms and a string with the room names comma-separated.

Related

SQL Query with two different filters

I have a page which shows all cars from DB.
I have two filters , both are multiple select filter.
For example
filter 1 - Color
Red , green , blue <-- All these are checkbox ,can be selected multiple
filter 2 - brand
BMW, Honda , Hyundai <-- All these are checkbox ,can be selected multiple
I have done below query
Select * from cars
JOIN term_car_relationships
ON cars.id = term_cars_relationships.car_id
WHERE term_cars_relationships.term_id in (6,2,3)
GROUP BY cars.id
In above query term_id
6 = blue (Color)
2 = green (Color)
3 = BNW (brand)
But with above query I will get all cars which has blue color or green color or BMW brand
But how to change in such a way that I get BMW which is blue or green color.
I have 3 tables which handles these categories.
taxonomy table
taxonomy_id | taxonomy_title
1 | Color
2 | Brand
term_list
term_id | term_name | taxonomy_id
1 | Blue | 1
2 | Red | 1
3 | BMW | 2
4 | Honda | 2
term_cars_relationships Table
term_id | car_id
1 | 1
1 | 2
2 | 3
You should join the term_cars_relationships table twice:
SELECT * FROM cars
JOIN term_car_relationships c ON cars.id = c.car_id
JOIN term_car_relationships b ON cars.id = b.car_id
WHERE c.type_of_category = 'color'
AND b.type_of_category = 'brand'
AND c.term_id in (6,2)
AND b.term_id in (3)
GROUP BY cars.id
Note that I used b.term_id in (3) instead of b.term_id = 3 since I assumed you might want to select multiple brands.
You can construct your query with separate joins for each term category, and separate filter conditions for each as well.
SELECT cars.*, colorTerms.*, brandTerms.*
FROM cars
INNER JOIN term_car_relationships AS carColorTerms
ON cars.id = carColorTerms.car_id
INNER JOIN term_list AS colorTerms
ON carColorTerms.term_id = colorTerms.term_id AND colorTerms.taxonomy_id = 1
INNER JOIN term_car_relationships AS carBrandTerms
ON cars.id = carBrandTerms.car_id
INNER JOIN term_list AS brandTerms
ON carBrandTerms.term_id = brandTerms.term_id AND brandTerms.taxonomy_id = 2
WHERE colorTerms.term_id IN (6, 2)
AND brandTerms.term_id IN (3)
Of course, to construct this query, you will need to know the terms' types before the query.
Also, using GROUP BY cars.id without aggregation is probably a sign of a problem, or just not the right way to get what you want. If you only want the information from the cars table, you should just SELECT DISTINCT cars.*. Using GROUP BY in this manner will end up with results with the data from just one color-brand match for each car.
With the complexity the edit to the original question added, another possibility presents itself....
SELECT cars.* -- You should really just select the fields you want, and may have to in some configurations (see GROUP BY)
FROM cars AS c
INNER JOIN term_car_relationships AS tcr ON c.car_id = tcr.car_id
INNER JOIN term_list AS tl ON tcr.term_id = tl.term_id
WHERE tcr.term_id IN (6, 2, 3)
GROUP BY cars.car_id -- In some configurations of MySQL, and most other RDBMSes, you must specify every SELECTed non-aggregated field here
HAVING COUNT(DISTINCT tl.taxonomy_id)
= ( SELECT COUNT(DISTINCT taxonomy_id)
FROM term_list
WHERE term_id IN (6, 2, 3)
)
Note: This final solution does not actually require you to know term taxonomies ahead of time anymore, and does not grow as more taxonomies need supported; so while it is a little less obvious with what it is doing, is probably definitely worth considering.

Select row in group by min of another column

Hey can you help me with my simple sql query ?
Tables
Hotel
id | name | country | rating
1 Bali bg 4.5
2 Beach uk 4.0
3 Blue sk 3.7
Tour
id | id_hotel | price
1 2 250
2 2 270
3 1 250
4 1 300
5 3 250
And I want something like this
SELECT t.* FROM tour t LEFT JOIN hotel h ON t.id_hotel = h.id
WHERE h.country = uk GROUP BY h.id ORDER BY t.price
So I wanna get only one row for each one hotel with min tour price.
And best with using Doctrine 2 queryBuilder.
Thanks for help.
Hey prbably I have solution
SELECT MIN(tour.id), hotel.* FROM `tour` INNER JOIN (
SELECT MIN(t.final_price) as final_price, t.id_hotel FROM tour t GROUP
BY t.id_hotel) ht
ON tour.id_hotel = ht.id_hotel AND tour.final_price =
ht.final_price
LEFT JOIN hotel ON hotel.id = tour.id_hotel GROUP BY tour.id_hotel
Use group by with order by to accomplish your result, try below query
select hotel.name,Tour.id,Tour.price,Hotel.rating from Hotel inner join Tour on Tour.id_hotel = Hotel.id group where hotel.country = 'uk' group by Tour.id_hotel order by Tour.price asc
Let me know if it helps
[Edit] This query should work with you

SQL reservation hotel room query

I want to create a reservation hotel's room using PHP.
My tables are: rooms and reservations:
rooms
room_id | seats
And I have 4 records in this table. 2x 2 seats room and 2x 4 seats room.
reservation
reservation_id | room_id | from_time | to_time
I want to choose free rooms at any given time.
My query did not take into account every second room.
SELECT *
FROM `rooms`
LEFT JOIN `reservation` ON `rooms.room_id` =`reservation.room_id`
WHERE
`seats` = 2
AND '01/01/2016'BETWEEN `reservation.from_time` AND `reservation.to_time`
or '11/02/2016' BETWEEN `reservation.from_time` AND `reservation.to_time`
Use backticks around the fields that have reserved names and also use braces around the conditions in the where clause
SELECT *
FROM `rooms` r
left outer join `reservation` rv on r.`room_id`=rv.`room_id`
where `seats`=2
AND (
'01/01/2016'BETWEEN rv.`from` AND rv.`to`
or
'11/02/2016' BETWEEN rv.`from` AND rv.`to`
)

MySql Select Result Having Combined Multiple Queries From Both the Same Table and Others

I have a table ('names') which includes data related with other data in other tables relying on ids. For example:
*Names table
id | name | predecessor | successor | house | birthplace
-----------------------------------------------------------------
10 Bayezid II 9 11 4 NULL
11 Selim I 10 12 4 5
12 Suleiman 11 13 4 61
*Houses table
id | house
--------------
4 House of Osman
*Places table
id | place
--------------
5 Amasya
61 Trabzon
What I'm trying to accomplish is to construct a query which results in returning whole information depending on the id, like:
{"result":[{
"id":"11",
"name":"Selim I",
"predecessor": "Bayezid II",
"successor": "Suleiman",
"house":"House of Osman",
"birthplace":"Amasya"
}]}
So, the name of the house and the birthplace are brought from other tables ('houses', 'places') whereas the predecessor and the successor are from the same table. I need help constructing this query. Thank you.
Just self-join a couple times, once to get the predecessor's row (aliased n0 below), and once more for the successor's (n2):
SELECT n1.id, n1.name, n0.name AS predecessor, n2.name AS successor
FROM names n1
LEFT JOIN names n0 ON n1.predecessor = n0.id
LEFT JOIN names n2 ON n1.successor = n2.id
SQL Fiddle demo
Joining to get the house and birthplace are left as an exercise for the reader.
Try this:
select n.id,
n.name,
n1.name as predecessor,
n2.name as successor,
h.house,
p.place
from names n
inner join names n1 on n.id = n1.predecessor
inner join names n2 on n.id = n2.successor
left join Houses h on n.house = h.id
left join Place p on n.birthplace = p.id
where n.id = 11

not listing users when using innerjoin with max in php mysql

i am new in php i have a problem in my php code.please help me..
i have two tables
seeker
seeker_nic-----username
111-------------ali
222-------------umer
333-------------raza
`
requestblood
id-------seeker_nic-----requireddate
1------- 111 ----------2012/9/9
2 ------- 222-----------2012/5/8
3 ------ 111-----------2012/10/11
4 ------- 111-----------2012/11/12
5 ------- 222-----------2012/7/9
6 ------- 333 ----------2012/4/4
now i want to list users one time with maximum date like..
s.no---- username----- requireddate
1------- ali---------- 2012/11/12
2------- umer--------- 2012/7/9
3------- raza--------- 2012/4/4
i am using this query
"SELECT bloodrequest.requireddate, seeker.username
FROM
bloodrequest
JOIN seeker ON bloodrequest.seeker_nic= seeker.seeker_nic
Join (SELECT max(requireddate)as maxdate FROM bloodrequest) maxresults on
bloodrequest.requireddate = maxresults.maxdate"
..
but it shows only 1 record not the list
and if use this query (left join instead of join)
"SELECT bloodrequest.requireddate, seeker.username
FROM
bloodrequest
left JOIN seeker ON bloodrequest.seeker_nic = seeker.seeker_nic
left join (SELECT max(requireddate)as maxdate FROM bloodrequest) maxresults
on bloodrequest.requireddate = maxresults.maxdate";
then it shows all records with all dates but not tha maximum..
id------seeker_nic -------requireddate<br>
1 ------ ali --------- 2012/9/9<br>
2 ------ ali ---------- 2012/10/11<br>
3 ------ ali ------------ 2012/11/12<br>
4------ umer------------- 2012/5/8<br>
5------- umer -------------2012/7/9<br>
6 ------ raza--------------2012/4/4<br>
You should be able to do this simpler:
select seeker.username, max(requestblood.requireddate)
from seeker
join requestblood on seeker.seeker_nic=requestblood.seeker_nic
group by seeker.username
Feel free to add any sorting order you need.

Categories