需要一个字段/标志/状态号以供多重使用?
我想在我的数据库中创建一个易于查询的字段。
我想如果我提供一些背景知识就会更有意义。我的桌子上显示了我的网站上的列表。我运行一个程序,它会查看列表并决定是否隐藏它们以使其不显示在网站上。我还出于各种原因手动隐藏列表。
我想将这些原因存储在一个字段中,这样就可以有多个隐藏原因。
所以我需要某种形式的逻辑来确定使用了哪些原因。
任何人都可以为我提供任何指导,说明什么是面向未来的,即新的原因,什么是快速且易于查询的?
I want to create a field in my database which will be easy to query.
I think if I give a bit of background this will make more sense. My table has listings shown on my website. I run a program which looks at the listings a decides whether to hide them from being shown on the site. I also hide listings manually for various reasons.
I want to store these reasons in a field, so more than one reason could be made for hiding.
So I need some form of logic to determine which reasons have been used.
Can anyone offer me any guidance on what will be future-proof aka new reasons and what will be quick and easy to query upon ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说
您可以通过为“多”侧提供一个指向“一”侧中的行之一的外键来实现“一对多”关系。在这种情况下,这意味着两个表:
列表可能有一个 id (int) 和一些文本 (text)。
Reason_to_hide_listing 将有自己的 id (int)、一个名为listing_id (int) 的外键列和一个名为reason (text) 的列。
要查明列表是否隐藏,请查询表 Reason_to_hide_listing 中是否有一个与您想知道是否应该显示的列表相同的列表 ID。如果返回一行或多行,则不应显示此列表。
要找出某个列表未显示的所有原因,请从表 Reason_to_hide_listing 中选择原因,其中外键是当前列表的 ID。
要隐藏列表,同时添加隐藏的原因,只需在reason_to_hide_listing中插入一个新行,其中的listing_id指向您要隐藏的列表。
you said
you implement the "one to many" relationship by giving the "many"-side a foreign key pointing to one of the rows in your "one" side. in this case this would mean two tables:
listing will probably have an id (int), and some text (text).
reason_to_hide_listing will have its own id (int), a foreign key column called listing_id (int), and a column named reason (text).
to find out if a listing is hidden, query the table reason_to_hide_listing for a listing_id equal to the listing you want to know if you should display or not. if one or more rows come back, this listing shouldn't be displayed.
to find out all the reasons why one listing isn't displayed, select the reason from the table reason_to_hide_listing where the foreign key is the id of the listing at hand.
to hide a listing, and at the same time add a reason to why it's hidden, just insert a new row in reason_to_hide_listing with a listing_id pointing to the listing you want to hide.
如果您希望允许用户(例如您自己:))提出自己的原因,请向表中添加一个
varchar(255)
可为空的字段来存储原因,然后查询您的原因使用where hide_reason is null
或其他内容来制作表格。如果您想要固定数量的原因,并且可以通过更改表定义偶尔更改这些原因,请使用
ENUM
字段:http://dev.mysql.com/doc/refman/5.0/en/enum.html。 (我不知道您正在使用哪个数据库,所以我将使用我最喜欢的一个)如果您想要固定数量的原因,您可以轻松更改这些原因,并且可以在下拉列表中显示您的用户可以选择,@davogotland 的解决方案确实是最好的。在数据库环境中添加表并不算过多;)
在任何情况下,为了清楚起见,您始终可以选择在
hide_reason
字段旁边使用单独的is_hidden
字段,或者因为您想记住某人再次取消隐藏字段或其他原因时隐藏该字段的原因。如果这样做,请使用BIT
/boolean
/TINYINT
字段。编辑:
啊,等等。我误读了上面的答案。您需要一组固定的原因,并且希望每个列表都能够有多个原因。这意味着您处于多对多的情况(每个列表可以有多个原因,每个原因可以分配给多个列表)。我知道有 3 种方法可以做到这一点:
SET
数据类型,它类似于ENUM
但其中可能包含多个值。请参阅: http://dev.mysql.com/doc/refman/5.0 /en/set.html。如果您使用的是 MS Access,则有一个“查找向导”数据类型,它似乎可以执行类似的操作(并且据说包括一个实际的查找向导)。reasons
和listings_reasons
,如下所示:reasons:
listings_reasons:
现在,在您的
reasons
表中填写可能的原因,并使用listings_reasons
表将它们链接到列表。这是最常见的解决方案,因为它允许您添加可能的原因而无需修改数据库结构。祝你好运!
If you want to allow a user (for example, yourself :) ) to come up with their own reason, add a
varchar(255)
nullable field to your table to store the reason, and then query your table your table usingwhere hide_reason is null
or something.If you want a fixed number of reasons that you can change occasionally by changing the table definition, use an
ENUM
field: http://dev.mysql.com/doc/refman/5.0/en/enum.html. (I don't know which database you're using, so I'll use my favorite one)If you want a fixed number of reasons that you can change with not too much effort, and that you can show in a dropdown list for your users to choose from, @davogotland's solution really is the best. Adding a table is not that excessive in a database environment ;)
In any case, you could always choose to use a separate
is_hidden
field alongside yourhide_reason
field, for clarity, or because you want to remember the reason someone hid a field also when they unhide it again, or whatever. If you do, use aBIT
/boolean
/TINYINT
field for this.EDIT:
Ah, wait. I misread the answer above. You want a fixed set of reasons, and you want each listing to be able to have more than one reason. This means you are in a many-to-many situation (each listing can have more than one reason, and each reason can be assigned to more than one listing). There are 3 ways to do this that I am aware of:
SET
data type, which is like anENUM
but with the possibility of having more than one value in it. see: http://dev.mysql.com/doc/refman/5.0/en/set.html. If you are using MS Access, there is a "Lookup Wizard" datatype which seems to do something similar (and supposedly includes an actual lookup wizard).reasons
andlistings_reasons
, which look like this:reasons:
listings_reasons:
Now, fill your
reasons
table with possible reasons, and link them to the listings using thelistings_reasons
table. This is the most common solution, since it allows you to add possible reasons without modifying your database structure.Good luck!
您可以使用枚举类型作为位标志,并将值作为 int 存储在数据库中。那么你所需要的只是一个表来存储相关页面的int和FK。
page.ReasonsToHide = HideReasons.SomeReason | page.ReasonsToHide = HideReasons.SomeReason | page.ReasonsToHide = HideReasons.SomeReason |隐藏原因.其他原因;
编辑:刚刚意识到您可能没有使用 C#。希望这无论如何都有帮助。
You could use an Enumeration type as bit flags and store the value as an int in the database. Then all you need is a table to store the int and a FK of the related page.
page.ReasonsToHide = HideReasons.SomeReason | HideReasons.OtherReason;
Edit: Just realized you may not be using C#. Hope this helps anyways.