简体   繁体   English

何时在数据库列中使用逗号分隔的值?

[英]When to use comma-separated values in a DB Column?

OK, I know the technical answer is NEVER . 好的,我知道技术答案是“ 永远”

BUT, there are times when it seems to make things SO much easier with less code and seemingly few downsides, so please here me out. 但是,有时候它似乎可以用更少的代码和更少的弊端使事情变得如此简单,所以请在这里告诉我。

I need to build a Table called Restrictions to keep track of what type of users people want to be contacted by and that will contain the following 3 columns (for the sake of simplicity): 我需要构建一个名为Restrictions的表来跟踪人们希望与之联系的用户类型,并且该表将包含以下3列(为简单起见):

minAge
lookingFor
drugs

lookingFor and drugs can contain multiple values. lookingFordrugs可以包含多个值。

Database theory tells me I should use a join table to keep track of the multiple values a user might have selected for either of those columns. 数据库理论告诉我,我应该使用联接表来跟踪用户可能为这些列之一选择的多个值。

But it seems that using comma-separated values makes things so much easier to implement and execute. 但是似乎使用逗号分隔的值使事情变得更容易实现和执行。 Here's an example: 这是一个例子:

Let's say User 1 has the following Restrictions: 假设用户1具有以下限制:

minAge => 18
lookingFor => 'Hang Out','Friendship'
drugs => 'Marijuana','Acid'

Now let's say User 2 wants to contact User 1 . 现在,假设用户2要联系用户1 Well, first we need to see if he fits User 1 's Restrictions, but that's easy enough EVEN WITH the comma-separated columns, as such: 好吧,首先,我们需要查看他是否符合用户1的限制,但这即使用逗号分隔的列也很容易,例如:

First I'd get the Target's ( User 1 ) Restrictions: 首先,我将获得目标的( 用户1 )限制:

SELECT * FROM Restrictions WHERE UserID = 1

Now I just put those into respective variables as-is into PHP: 现在,我将它们原样放入PHP的各个变量

$targetMinAge = $row['minAge'];
$targetLookingFor = $row['lookingFor'];
$targetDrugs = $row['drugs'];

Now we just check if the SENDER ( User 2 ) fits that simple Criteria: 现在,我们只检查SENDER( 用户2 )是否符合该简单条件:

COUNT (*) 
   FROM Users
WHERE 
   Users.UserID = 2 AND
   Users.minAge >= $targetMinAge AND
   Users.lookingFor IN ($targetLookingFor) AND
   Users.drugs IN ($targetDrugs)

Finally, if COUNT == 1, User 2 can contact User 1 , else they cannot. 最后,如果COUNT == 1,则用户2可以联系用户1 ,否则他们不能。

How simple was THAT ? 如何简单的是什么 It just seems really easy and straightforward, so what is the REAL problem with doing it this way as long as I sanitize all inputs to the DB every time a user updates their contact restrictions? 这看起来确实很容易和直接,所以只要每次用户更新其联系限制时我都清理了对DB的所有输入,用这种方法实现的真正问题是什么? Being able to use MySQL's IN function and already storing the multiple values in a format it will understand (eg comma-separated values) seems to make things so much easier than having to create join tables for every multiple-choice column. 能够使用MySQL的IN函数并已经以它会理解的格式存储了多个值(例如,逗号分隔的值),这似乎比必须为每个选择项列创建联接表要容易得多。 And I gave a simplified example, but what if there are 10 multiple choice columns? 我给出了一个简化的示例,但是如果有10个多选栏怎么办? Then things start getting messy with so many join tables, whereas the CSV method stays simple. 然后,这么多的联接表开始变得混乱,而CSV方法保持简单。

So, in this case, is it really THAT bad if I use comma-separated values? 因此,在这种情况下,如果我使用逗号分隔的值真的不好吗?

****ducks**** ****鸭子****

You already know the answer. 您已经知道答案了。

First off, your PHP code isn't even close to working because it only works if user 2 has only a single value in LookingFor or Drugs. 首先,您的PHP代码甚至无法使用,因为仅当用户2在LookingFor或Drugs中只有一个值时,它才起作用。 If either of these columns contains multiple comma-separated values then IN won't work even if those values are in the exact same order as User 1's values. 如果这些列中的任何一个包含多个逗号分隔的值,那么即使这些值与用户1的值完全相同,也无法使用IN。 What do expect IN to do if the right-hand side has one or more commas? 如果右侧有一个或多个逗号,IN应该怎么做?

Therefore, it's not "easy" to do what you want in PHP. 因此,在PHP中完成所需的工作并不容易。 It's actually quite a pain and would involve splitting user 2's fields into single values, writing dynamic SQL with many ORs to do the comparison, and then doing an extremely inefficient query to get the results. 这实际上很麻烦,需要将用户2的字段拆分为单个值,使用多个OR编写动态SQL进行比较,然后执行效率极低的查询来获取结果。

Furthermore, the fact that you even need to write PHP code to answer such a relatively simple question about the intersection of two sets means that your design is badly flawed. 此外,您甚至需要编写PHP代码来回答关于两个集合的交集的相对简单的问题,这意味着您的设计存在严重缺陷。 This is exactly the kind of problem (relational algebra) that SQL exists to solve. 正是 SQL存在要解决的问题 (关系代数)。 A correct design allows you to solve the problem in the database and then simply implement a presentation layer on top in PHP or some other technology. 正确的设计可以让您解决数据库中的问题,然后只需在PHP或某些其他技术的顶部实现一个表示层。

Do it correctly and you'll have a much easier time. 正确执行此操作,您将拥有更加轻松的时间。

Suppose User 1 is looking for 'Hang Out','Friendship' and User 2 is looking for 'Friendship','Hang Out' 假设用户1正在寻找“ Hang Out”,“ Friendship”,而用户2正在寻找“ Friendship”,“ Hang Out”

Your code would not match them up, because 'Friendship','Hang Out' is not in ('Hang Out','Friendship') 您的代码与它们不匹配,因为'Friendship','Hang Out'未加入('Hang Out','Friendship')

That's the real problem here. 这是真正的问题。

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

相关问题 在单个查询中搜索逗号分隔的MySQL列中的多个值 - Searching multiple values in a Comma-Separated MySQL column in a single query 将数组列中的唯一值打印为逗号分隔的字符串 - Print unique values from an array column as a comma-separated string 按一列对子数组进行分组,并在组内将另一列与逗号分隔的值 - Group subarrays by one column, make comma-separated values from other column within groups 按一列对子数组数据进行分组,并与每组中的辅助值形成逗号分隔的值 - Group subarray data by one column and form comma-separated values from the secondary value in each group 对值进行分组并将索引存储为逗号分隔值 - Group values and store indexes of as comma-separated values 隔离逗号分隔值,删除重复值和空值,然后排序 - Isolate comma-separated values, remove duplicates and empty values, then sort 用于逗号分隔列的Prestashop管理面板自定义筛选器 - Prestashop admin panel custom filter for comma-separated column 从对象数组的单列创建逗号分隔的字符串 - Create a comma-separated string from a single column of an array of objects 在excel列上应用正则表达式后创建逗号分隔的文本 - Creating comma-separated text after applying regex on excel column 如何计算PHP中逗号分隔值? - How do I count comma-separated values in PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM