Search code examples
phpmysqlsqlrowmultiple-columns

SQL check if row contains set of values


I have a Database like the following: enter image description here

I now want to check each row in this table if it contains a set of data. like so: I want to query for the following letters: A, B
this should return the following rows: res1, res4
this is because both res1 and res4 contain the values A and B in one of the columns

this logic also applies in this case:
and if i would only query for the letter B the query must return the following rows: res1, res2, res4

So what i want to happen is that the query checks the columns valsx, valsy, valsz, valsq if either one of these columns contains one of the queried values. And if all the queried values exist in the row (the four columns) then return the row.

How would i do this with SQL i have no idea. I tried select * from table where valsx contains b or valsy contains b or valsz contains b or valsq contains b

if anything is unclear let me know so i can clarify.


Solution

  • You can use in:

    select t.*
    from t
    where 'A' in (valsx, valsy, valsz, valsq) and
          'B' in (valsx, valsy, valsz, valsq);