简体   繁体   中英

Mysql - Best way to store yes/no recurring details about items in table

I have a table in db with a list of apartments, and each apartment has amenities.

I use to save the amenities in the apartments list table, but its a problem because sometimes i need to add/ edit amenities, and apartment with no amenities take unnecessary fields.

The table looks like this (with more amenities):

+----+---------+---------+-------+------+
| id | Address | Doorman | Cable | Pets |
+----+---------+---------+-------+------+
|  1 | E 55th  |       1 |     0 |    1 |
|  2 | E 72th  |       0 |     1 |    1 |
+----+---------+---------+-------+------+

Which other way could i store this kind of data?

Probably the correct way is to store the amenities in a different table, but then how i query apartment by amenities?

Now to query i do Select * from table where Cable=1

The correct way is to create another table (eg amenities). Let's say we have these tables:

CREATE TABLE `apartments` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `address` VARCHAR(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `amenities` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `amenity_id` enum('doorman','cable','pets') NOT NULL,
  `apartment_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_amenities_apartments_idx` (`apartment_id`),
  CONSTRAINT `fk_amenities_apartments` FOREIGN KEY (`apartment_id`) REFERENCES `apartments` (`id`)
);

The the query will be like this:

select ap.*
from apartments as ap
    left join amenities as am on (ap.id = am.apartment_id)
where am.amenity_id = 'cable';

Of course, you can use another table structure for amenities, but the general principle will be the same.

Feel free to ask to improve the answer.

IMHO the best way to store these informations is to create a three tables view. First table will just store apartment infoes. Second table will be a dictionary of amenities that will convert each amenity into an id (id_amenity/amenity_name) (table named: amenities ). Last you will have a table where you will store the match between amenities and apartment (id_apartment/id_amenity) (table named: apar_amen ). To retrieve the information you will query the last table joining it to the other two:

SELECT apartments.*, amenity_name FROM apar_amen 
JOIN apartments ON apar_amen.id_apartment = apartments.id 
JOIN amenities ON apar_amen.id_amenity=amenities.id_amenity
WHERE amenity_name = '1' //where 1 = 'Cable' in amenities list

This way you can add as many amenities as you want, change their names and have any apartment have just as many records as needed. This way the links are made only by id and your code will still work if you change "Cable" to "Cable TV" for example since "Cable" is just a label and not a column name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM