Home > OS >  Query that checks for a value A to appear at least B number of times in C columns
Query that checks for a value A to appear at least B number of times in C columns

Time:10-30

I'm trying to select rows from a table where some value a is within c columns at least b times.

Here is a simplified example of the data I'm working with. I'm trying to find rows that have an h in at least 2 of the greetings.

greeting1 greeting2 greeting3 farewell1 farewell2
hi hello hey there goodbye peace
hi sup yo Au revoir see ya
yo hola ayyyy hang tight reverse-hi
hola sup hello see ya peace
hello yo hola hang tight ciao
ayyy yo hola hang tight ciao

And below are the rows I'd like to grab:

greeting1 greeting2 greeting3 farewell1 farewell2
hi hello hey there goodbye peace
hola sup hello see ya peace
hello yo hola hang tight ciao

(I italicized and bolded for easier ability to see)

I'm just learning SQL, so this might be basic. I was having some difficulties. I know getting the rows and checking all greeting1 would look something like:

SELECT *
FROM
  GreetingsAndFarewell
WHERE
  greetings1 LIKE '%h%'
  AND greetings2 LIKE '%h%'
  AND greetings3 LIKE '%h%'

But what if I only care if at least 2 greetings have an h in them?

Here are some of the similar questions I've seen, but I don't think either of them really address this:

enter image description here

  • Related