简体   繁体   中英

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. 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).

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)
  • 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...)
  • 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

Does SQL support this kind of query? 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. 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 . 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. 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.)

Ideally you would normalize the schema, but that's often not possible.

To find records with both mammal and carnivore :

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')

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