1

I have two tables one for units and one for Amenities

Table units
+---------+---------------------+---------------------+-----------+----------+
| unit_id |     date added      |     date modified   | unit name |   user   |
+---------+---------------------+---------------------+-----------+----------+
|       1 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 |  Villa 1  | Smith    |
|       2 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 |  Villa 2  | Smith    |
|       3 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 |  Villa 3  | Jones    |
|       4 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Apartment | Smith    |
+---------+---------------------+---------------------+-----------+----------+

Table Amenities       
+---------+-----------+-------------------+
|      id |  Unit_id  |   Amenity         |
+---------+-----------+-------------------+
|       1 |         1 |   Air conditions  |
|       2 |         1 |   Internet        |
|       3 |         1 |   Water heaters   |
|       4 |         1 |   TV              |
|       5 |         2 |   TV              |
|       6 |         2 |   pool            |
|       7 |         2 |   Internet        |
|       8 |         3 |   Internet        |
|       9 |         4 |   Internet        |
+---------+-----------+-------------------+

i want to select units where it has both TV and Internet

I try

select units.* from units left join Amenities  on units.unit_id=Amenities.Unit_id  

where Amenities.Amenity='TV' and  Amenities.Amenity='Internet'

but not work

10
  • 1
    what error comes from this query? Commented Nov 13, 2017 at 11:18
  • return empty string while it should return to units Commented Nov 13, 2017 at 11:21
  • you are passing And clause thats y Commented Nov 13, 2017 at 11:21
  • do OR there, what you want exactly? a unit with both the Amenity or one of the both? Commented Nov 13, 2017 at 11:22
  • 1
    @Wodin - yes - or if you want to find units that have 2 out of 3. Commented Nov 13, 2017 at 12:06

2 Answers 2

1
DROP TABLE IF EXISTS amenities;

CREATE TABLE amenities
(unit_id INT NOT NULL
,amenity VARCHAR(50) NOT NULL
,PRIMARY KEY(unit_id,amenity)
);

INSERT INTO amenities VALUES
(1,'Air conditions'),
(1,'Internet'),
(1,'Water heaters'),
(1,'TV'),
(2,'TV'),
(2,'pool'),
(2,'Internet'),
(3,'Internet'),
(4,'Internet');

SELECT unit_id 
  FROM amenities 
 WHERE amenity IN ('TV','Internet') 
 GROUP 
    BY unit_id 
HAVING COUNT(*) = 2;
+---------+
| unit_id |
+---------+
|       1 |
|       2 |
+---------+

Obviously, you'd never have a table like the one you describe. Instead, you'd have a table of unit, a table of amenities, and a table which says which amenity belongs to which unit.

Sign up to request clarification or add additional context in comments.

2 Comments

I agree that it's better to have an Amenties table that only has amenity_id and amenity columns and then a linking table with unit_id and amenity_id. Then Amenities would only have one record for e.g. Internet, but there would be a record in unit_amenities for each unit with the Internet amenity.
@Wodin Exactly.
0
SELECT ua.*
FROM units AS ua
INNER JOIN units AS ub ON ua.unit_id = ub.unit_id
INNER JOIN Amenities AS aa
    ON ua.unit_id = aa.Unit_id
    AND aa.Amenity = 'TV'
INNER JOIN Amenities AS ab
    ON ub.unit_id = ab.Unit_id
    AND ab.Amenity = 'Internet';

Here's the output generated with your data:

+---------+---------------------+---------------------+-----------+-------+
| unit_id | date added          | date modified       | unit name | user  |
+---------+---------------------+---------------------+-----------+-------+
|       1 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 1   | Smith |
|       2 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 2   | Smith |
+---------+---------------------+---------------------+-----------+-------+

1 Comment

Thanks for helping me, i found that his answer is petter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.