Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Select with LIKE and WHERE

I have a table of biographical data, and I need to do a query that selects people who have the word "died" in their bio and whose death date is NULL. This doesn't work:

SELECT * FROM people
WHERE bio LIKE '%died%'
AND death_date IS NULL

That selects everyone whose death_date is null, but also selects people who don't have the word "died" in their bio. Can I do this in one query?

like image 341
kirkaracha Avatar asked Nov 19 '10 16:11

kirkaracha


People also ask

Does MySQL have Ilike?

ILIKE doesn't exist in mysql.

Can you grep in MySQL?

Common Resource Grep (crgrep) searches for table/column name and data matches and supports MySQL. Also searches other hard to search resources like content buried in archives.

What is the difference between like and Ilike?

LIKE and ILIKE allow pattern matching within character-based column data. Their syntax is identical, but LIKE is case-sensitive, while ILIKE is case-insensitive.

How do I match a pattern in MySQL?

Use the LIKE or NOT LIKE comparison operators instead. The other type of pattern matching provided by MySQL uses extended regular expressions. When you test for a match for this type of pattern, use the REGEXP_LIKE() function (or the REGEXP or RLIKE operators, which are synonyms for REGEXP_LIKE() ).


2 Answers

It could be a word like 'completelydied' and they are still going to be selected. Check carefully if 'died' phrase does not exists as part of some word in the records that you got selected.

like image 58
Yasen Zhelev Avatar answered Sep 27 '22 19:09

Yasen Zhelev


Perhaps the problem is the quotes, use " instead of ' to wrap the LIKE clause.

SELECT * FROM people
WHERE bio LIKE "%died%"
AND death_date IS NULL
like image 33
Webnet Avatar answered Sep 27 '22 20:09

Webnet