jdero - 8 months ago 26

SQL Question

My question seems to be quite simple, but I'm worried the answer might actually be somewhat complex. I am trying to perform a simple Select query that behaves like the following.

Here is the code:

`SELECT * FROM tbl_tbl WHERE tbl_tbl.colA LIKE '%foo%' OR tbl.tbl.colA LIKE '%oof%' AND`

tbl_tbl.colB LIKE '%bar%' OR tbl_tbl.colB LIKE '%rab%'

So I am just searching for 4 strings (2 in each column), and if I find one in each pair, I want to show that entire entry.

Mathematically, it makes quite a bit of sense to me.

Pretty simple right?

How do I tell SQL to work

Currently, I'm getting entries out of my table where only 1 of the column disciplines match, and that's not giving me the specificity of the priority I am looking for.

Answer

You would express it using parentheses and boolean logic in the `where`

clause:

```
SELECT *
FROM tbl_tbl t
WHERE (t.colA LIKE '%foo%' OR t.colA LIKE '%oof%') AND
(t.colB LIKE '%bar%' OR t.colB LIKE '%bar%');
```

Do note that this is based on your example in the question. The second clause of the `AND`

has two conditions that are the same. I assume this is a typo in the question, but not knowing the right pattern, I've left it in the answer.