less than 1 minute read

Problem

problem-1527

Need to review

Query

SELECT
    *
FROM
    Patients
WHERE
    conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%'

Editorial Solution

Approach 1: Using Regular Expression Word Boundaries

Note: '\\bDIAB1.*': The regular expression itself. Let’s break it down:

  • \\b: Represents a word boundary. It ensures that “DIAB1” is treated as a whole word and not part of a larger word.
  • DIAB1: Matches the literal string “DIAB1”.
  • .*: Matches any characters (except for a newline) zero or more times.

So, the query selects rows from the “Patients” table where the “conditions” column contains a whole word “DIAB1” followed by any characters. This would match rows where “conditions” might have values like “DIAB1XYZ” or “DIAB1abc”, but it would not match values like “SADIAB100” because of the word boundary requirement.

SELECT patient_id, patient_name, conditions
FROM Patients
WHERE conditions REGEXP '\\bDIAB1.*';

Tags:

Updated: