简体   繁体   English

MySQL-存储有关表中项目的是/否重复发生详细信息的最佳方法

[英]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. 我在db中有一张桌子,上面有公寓的清单,每间公寓都有便利设施。

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 现在查询我是否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 ). 第二个表将是设施,将每市容转换成ID(id_amenity / amenity_name)(表命名为:一本字典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 ). 最后,您将有一个表,用于存储设施和公寓之间的匹配项(id_apartment / id_amenity)(表名为: 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. 这样,仅通过id进行链接,并且如果将“ Cable”更改为“ Cable TV”,您的代码仍然可以使用,例如,因为“ Cable”仅是标签而不是列名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 最好的方法在SQL表中存储包含详细信息的项目数组 - Best way store an array of items with details in SQL table 一个或多个数据库表设计中的mySQL存储文件详细信息以及创建父级和子级类别的最佳方法 - mySQL store file details in one or many database table design and best way of creating parent and child categories 在MySQL表中存储组成员的最佳方法 - best way to store member of group in mysql table 从MySQL中的数据库表中以分层方式删除项目的最佳方法? - The best way to delete items in hierarchy way from a DB table in mysql? 从mysql表中的php存储类别名称的最佳方法 - Best way to store a category name from php in a mysql table 在 MYSQL 中的父表中存储子项数的最佳方法 - Best way to store number of children in parent table in MYSQL 在MySQL的其他表中存储对行的可搜索引用的最佳方法 - Best Way to Store Searchable References to Rows in Other Table in MySQL 存储与 mysql 表的列相关的值的最佳方法? - Best way to store values related to columns of a mysql table? Mysql 在表中存储任意数量的值的最佳方式 - Mysql Best way to store an arbitrary number of value in a table 在MySQL表中存储历史价格表的最佳方法是什么? - What is the best way to store a historical price list in a MySQL table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM