My table contains a column name title. I want to sql search filter male and female from title column

1k Views Asked by At

I have a sql database table name law. Which have a column name title. I want to implement search operation using LIKE query. But when I search for male from title column it also returns female. One thing to notify that my title column contains multiple of words. But i dont want to see those rows which contains female word. I used wildcard for LIKE parameter.

SELECT * FROM `laws` WHERE title LIKE "male"
2

There are 2 best solutions below

1
nbk On BEST ANSWER

You need to include male and explicit exclude female from the text.

If you want the second line you need somewhat more complicated query

CREATE TABLE laws (title varchar(50))
INSERT INTO laws VALUEs ('Only for male partitipents'),('male and female partitipents'),('No more female partitipents in this law')
SELECT * FROM `laws` WHERE title LIKE '%male%' AND title NOT LIKE  '%female%'
| title                      |
| :------------------------- |
| Only for male partitipents |

db<>fiddle here

0
Michał Bielecki On

Question is do You need to use like from what I read You need to use:

SELECT * FROM law WHERE title = 'male';

Is You need to use like I would use:

SELECT * FROM law WHERE title like 'male%';

Of course if You are looking for records like: male age 25.