Retrieving specific tuples using Mysql - mysql

I have some problems retrieving specific tuples.
I am actually a student trying to build a Room management system. I have two tables:
Room(roomID,hotelname,rate)
and
Reservation(resID,arriveDate,departDate,roomID).
I am not sure how to retrieve the rooms that are available between 2 specific dates.
This was the query that i used.
SELECT Room.roomID,hotelname,rate
FROM Room
LEFT JOIN Reservation
on ( Room.roomID=Reservation.resID
and arriveDate >='2010-02-16'
and departDate <='2010-02-20'
)
GROUP BY roomID,hotelname,rate
HAVING count(*)=0;'
but it returns an empty set. Can any1 be kind enough to tell me what mistake i am doing??

I guess Room.roomID=Reservation.resID should be Room.roomID=Reservation.roomID.
You could try a different approach with a subselect:
SELECT roomID,hotelname,rate
FROM Room
WHERE roomID NOT IN (SELECT roomID FROM Reservation WHERE arriveDate >='2010-02-16' and departDate <='2010-02-20')

Related

ORACLE Query occurrence for an element

I'll try to explain with a generic example:
I have a table in MySQL with two columns: "Product" and "Client". For each product a client buy, a register is generated in this table.
Now I took 2 products, Stove and Refrigerator, and I want to find the client that bought both products and just them.
In this case, the expected result would be Marley, and why not Maria too? Because beyond the stove and refrigerator she also bought a chair.
How could be a MySQL ORACLE select query to give me that result?
edit: the question was wrong, it's Oracle and not MySQL, thanks Gordon Linoff
You can use aggregation. Assuming there are no duplicates in the table:
select client
from t
group by client
having sum( product in ('Stove', 'Refrigerator') ) = 2 and
count(*) = 2;
If there are duplicates, the logic is:
select client
from t
group by client
having count(distinct case when product in ('Stove', 'Refrigerator') then product end ) = 2 and
count(distinct product) = 2;
Concept Used:
Filter record for Stove and Refrigerator.
Group records by Client
Each Client should have exactly two distinct records, if yes then it should be in result set.
SELECT Client
FROM table
WHERE Product IN ('Stove', 'Refrigerator')
GROUP BY Client
HAVING COUNT(DISTINCT Product) = 2

Query to obtain the lowest price per room per hotel in a relational database

I have a relational database model based on a hotel booking site set up for a uni project, however I am stumped by one query.
This is as far as I have gotten:
SELECT DISTINCT
property.property_id,
property_name,
property_description,
star_rating,
room_base_price
FROM
property
INNER JOIN rooms ON property.property_id = rooms.property_id;
The problem is that I have several rooms per property listed in my database. In this query, I want to select the room with the minimum price per hotel. The way I am currently doing it, it is showing every single property with every single room and its individual price.
Can anyone advise me on how I can write a query where I will be able to return the lowest price of a room in a property(i.e. a hotel), where it will only be one row per hotel instead of returning multiple rows for each property?
Try following query:
SELECT DISTINCT
property.property_id,
property_name,
property_description,
star_rating,
room_base_price
FROM
property
INNER JOIN rooms ON property.property_id = rooms.property_id;
where (rooms.property_id,rooms.room_base_price) in
(select property_id,min(room_base_price)
from rooms
group by property_id
)
;
Hope it helps!

count number of repeating entries

I am fairly new to Databases and I am just beginning to understand the DML/queries, I have two tables, one named customer this contain customer data and one named requested_games, this contains games requested by the customers, I would like to write a query that will return the customers that have requested more than two games, so far when I run the query, I don't get the desired result, not sure if I'm doing it right.
Can anyone assist with this thanks,
Below is a snippet of the query
select customers.customer_name, wants_list.requested_game, wants_list.wantslists_id,count(wants_list.customers_ID)
from customers, wants_list
where customers.customers_ID = wants_list.customers_id
and wants_list.wantslists_id = wants_list.wantslists_id
and wants_list.requested_game > '2';
just include a HAVING clause
GROUP BY customers_ID
HAVING COUNT(*) > 2
depending on how you have your data setup you may need to do
HAVING COUNT(wants_list.requested_game) > 2
This is how I like to describe how a query works maybe itll help you visualize how the query executes :)
SELECT is making an order at a restaurant....
FROM is the menu you want to order from....
JOIN is what sections of the menu you want to include
WHERE is any customization you want to make to your order (aka no mushrooms)....
GROUP BY (and anything after) is after the order has been completed and is at your table...
GROUP BY tells your server to bring your types of food together in groups
ORDER BY is saying what dishes you want first (aka i want my entree then dessert then appetizer ).
HAVING can be used to pick out any mushrooms that were accidentally left on the plate....
etc..
I would like to write a query that will return the customers that
have requested more than two games
For this to happen you need to do the following
First you need to use GROUP BY to group the games based on customers (customers_id)
Then you need to use HAVING clause to get customers who requested more than two games
Then make this a SUBQUERY if you need more information on the customer like name
Finally you use a JOIN between customers and the sub query (temp) to display more information on the customer
Like the following query
SELECT customers.customer_id, customers.customer_name, game_count
FROM (SELECT customer_id, count(wantslists_id) AS game_count
FROM wants_list
GROUP BY customer_id
HAVING count(requested_game) > '2') temp
JOIN customers ON customers.customer_id = temp.customer_id

Finding the sum of a field in a linked table per group with additional search criteria

My actual tables are much more complex but here is a simplified example of the problem I am trying to work out.
Table contact: ContactID, ContactName, Pending
Table purchase: PurchaseID, ContactID, Amount, Pending, Date
Table contact_purchase_link: ContactID, PurchaseID (although it may seem like the link table is not necessary in this simplified example it is necessary in the large table schema)
Here is the query that I currently have:
SELECT DISTINCT contact.ContactID,
( SELECT SUM(Amount)
FROM purchase
WHERE purchase.ContactID = contact.ContactID
AND purchase.Pending = 0
) totalpurchase
FROM contact
INNER JOIN ( contact_purchase_link JOIN purchase
ON (contact_purchase_link.PurchaseID = purchase.PurchaseID
))
USING (ContactID)
WHERE purchase.Date > '2013-12-06' AND
AND contact.Pending =0
The problem is that I want the totalpurchase (the sum of the amount field) to be limited to the search criteria of the purchase table - meaning the query should only return the sum of the purchases after the specified date per contact. I think in order to use a group by clause the query would have to be based off the purchase table but I need the query to use the contact table so that all contacts are listed with their total purchase amounts and other relevant client data.
Is there any way to do this within one query?
To further clarify:
This query is being generated as part of a search engine. An example of why a query like this would be done is if a user wanted to generate a contact list of lastnames starting with A with purchases of a specific item or as in this example of purchases for a specific date. So that in general the query would have to generate a list of all contacts and their data (with possible search criteria on the type of contact such as all lastnames starting with 'A' etc.) and the query can also include search criteria on the purchase table such as the date of the purchase and whether the purchase was for specific items etc.
I am trying to add in the option to also list the sum of the purchases for the contact however that sum has to be limited to the search criteria for the purchase table as well and not the sum of all the contacts purchases.
If I understand your question correctly, you need to move the date comparison inside the first subquery:
SELECT DISTINCT contact.ContactID,
( SELECT SUM(Amount)
FROM purchase
WHERE purchase.ContactID = contact.ContactID
AND purchase.Pending = 0
AND purchase.Date > '2013-12-06'
) totalpurchase
FROM contact
INNER JOIN ( contact_purchase_link JOIN purchase
ON (contact_purchase_link.PurchaseID = purchase.PurchaseID
)
USING (ContactID)
WHERE purchase.Date > '2013-12-06'
AND contact.Pending =0
But the comments are right - I corrected a couple of what appears to be syntax errors, and I'm not sure about the join to contact_purchase_link. Improve your question and my answer will be less like guesswork.

MySQL innerjoin 3 tables

Supposing I had 3 tables
Passenger (Id [pk], name)
Airplane (registration [pk], num_seats)
Booking (Id, passenger_id [fk -> Passenger.Id], airplane_registration [fk -> Airplane.registration])
The booking table would be a junction object here as there is a many to many relationship between tables Passenger and Booking.
How could I select all the details related to a particular booking id (say 'abcde')?
Is this correct:
SELECT Passenger.name, Airplane.num_seats, Booking.Id
FROM Booking
JOIN Passenger ON Passenger.Id = Booking.passenger_Id
JOIN Airplane ON Booking.airplane_registration = Airplane.registration
WHERE Booking.Id = 'abcde';
Is this the right way to do it?
Also, If I wanted to select all bookings and their details, would I do it the same way? (Without the where clause)?
I was looking to find out if this was correct for a test as MySQL has gone down on my machine.
Yes, your query would work for getting the details of the passengers and the flight for the particular booking ID. For getting all bookings, I would add an ORDER BY bookingID and if needed by passenger name and flight registration.
Without knowing your schema, there's no way to be sure, but that looks just fine to me.