简体   繁体   English

如何在SQL中查询单个单元格中的多个值

[英]How to query for multiple values in a single cell in SQL

newbie here.新手在这里。 Apologies in advance if I don't use the proper terminology.如果我没有使用正确的术语,请提前道歉。

SITUATION: I'm working with my first SQL database, and the cells in one column have multiple values, many of which overlap with other cells.情况:我正在使用我的第一个 SQL 数据库,并且一列中的单元格具有多个值,其中许多值与其他单元格重叠。 Here's an excerpt (I'm new to stack overflow and can't insert images yet, and I can't figure out the best way to represent the layout in text, but here's my best attempt):这是摘录(我是堆栈溢出的新手,还不能插入图像,我无法找出用文本表示布局的最佳方式,但这是我的最佳尝试):

 aardvark | animal mammal carnivore
 tiger    | animal carnivore mammal
 snail    | animal mollusk

QUESTION: How do I call all rows that have a combination of specific multiple values, but not necessarily in the order queried?问题:如何调用具有特定多个值组合但不一定按查询顺序排列的所有行?

EXAMPLE OF the DESIRED OUTCOME: Using the columns/values listed above, I want to pull only rows that have BOTH 'animal' and 'mammal' values in column 2 (so row_1 and row_2, but not row_3).预期结果示例:使用上面列出的列/值,我只想提取第 2 列中同时具有“动物”和“哺乳动物”值的行(即 row_1 和 row_2,但不是 row_3)。

EXAMPLES OF FAILED/CONFUSING ATTEMPTS (using the columns/values listed above):失败/混淆尝试的示例(使用上面列出的列/值):

  • if I do: SELECT * FROM table_name WHERE col_name LIKE '%mammal%' AND '%animal%' , zero rows are returned (which happens regardless of whether '%mammal%' or '%animal%' is listed first)如果我这样做: SELECT * FROM table_name WHERE col_name LIKE '%mammal%' AND '%animal%' ,则返回零行(无论 '%mammal%' 或 '%animal%' 是否首先列出,都会发生这种情况)
  • if I do SELECT * FROM table_name WHERE col_name LIKE '%animal%' OR '%mammal%' , all three rows are returned (which I expected to happen, but...)如果我执行SELECT * FROM table_name WHERE col_name LIKE '%animal%' OR '%mammal%' ,则返回所有三行(我预计会发生,但是...)
  • if I switch the LIKE conditions while using OR ( SELECT * FROM table_name WHERE col_name LIKE '%mammal%' OR '%animal%' ), only rows 1 and 2 are returned如果我在使用 OR ( SELECT * FROM table_name WHERE col_name LIKE '%mammal%' OR '%animal%' ) 时切换 LIKE 条件,则只返回第 1 行和第 2 行

Does SQL support this kind of query? SQL 是否支持这种查询? I've spent the past couple of days searching for a solution, and the closest I got was I think ALL might have something to do with it, but if that is the case I haven't been able to figure out the right syntax.过去几天我一直在寻找解决方案,我得到的最接近的是我认为 ALL 可能与它有关,但如果是这种情况,我一直无法找出正确的语法。 I've looked around and found some articles about how to combine multiple column values into one cell, but not how to search for them.我环顾四周,发现了一些关于如何将多个列值组合到一个单元格中的文章,但没有找到有关如何搜索它们的文章。 I considered reformatting the table so the column has only one value per cell, but that would require adding a bunch more columns to accommodate all of the values, and it seems like that would make searching a lot more complicated because it would require searching all of those columns instead of just one.我考虑重新格式化表格,以便每个单元格的列只有一个值,但这需要添加更多列来容纳所有值,而且这似乎会使搜索变得更加复杂,因为它需要搜索所有那些列而不是一列。

Thanks!谢谢!

First, you should understand that you have a broken data model.首先,您应该了解您的数据模型已损坏。 Storing multiple values in a string is simply not the right way to store data in a database.在字符串中存储多个值根本不是在数据库中存储数据的正确方法。 You should have a table with one row per animal and feature.您应该有一个表格,每个动物和功能各占一行。

That said, sometimes we are stuck with other people's really, really bad decisions.也就是说,有时我们会被其他人非常非常糟糕的决定所困。 You can handle the situation using like .您可以使用like处理这种情况。 The simplest approach is:最简单的方法是:

where col_name like '%mammal%' and col_name like '%animal%'

However, this has a problem, because you are not taking the delimiters into account -- you would want "ant" to match "anteater", presumably.但是,这有一个问题,因为您没有考虑分隔符 - 您可能希望“ant”与“anteater”匹配。 So:所以:

where concat(' ', col_name, ' ') like '% mammal %' and
      concat(' ', col_name, ' ') like '% animal %'
      

This is a generalized approach that should work with any database without modifying the schema or data.这是一种通用方法,可以在不修改架构或数据的情况下使用任何数据库。 It will not perform well, however.然而,它不会表现得很好。 (You could also consider a RegEx approach if your db supports it.) (如果您的数据库支持,您也可以考虑使用 RegEx 方法。)

Ideally you would normalize the schema, but that's often not possible.理想情况下,您将规范化架构,但这通常是不可能的。

To find records with both mammal and carnivore :要查找mammalcarnivore记录:

select ...
from ...
WHERE (col_name = 'mammal'
        or col_name like 'mammal %'
        or col_name like '% mammal %'
        or col_name like '% mammal')
    and (col_name = 'carnivore'
        or col_name like 'carnivore%'
        or col_name like '% carnivore%'
        or col_name like '% carnivore')

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

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