LIKE Predicate

Description

A LIKE predicate is used to search for a specific pattern. This predicate also supports multiple patterns with quantifiers include ANY, SOME and ALL.

Syntax

  1. [ NOT ] { LIKE search_pattern [ ESCAPE esc_char ] | [ RLIKE | REGEXP ] regex_pattern }
  2. [ NOT ] { LIKE quantifiers ( search_pattern [ , ... ]) }

Parameters

  • search_pattern

    Specifies a string pattern to be searched by the LIKE clause. It can contain special pattern-matching characters:

    • % matches zero or more characters.
    • _ matches exactly one character.
  • esc_char

    Specifies the escape character. The default escape character is \.

  • regex_pattern

    Specifies a regular expression search pattern to be searched by the RLIKE or REGEXP clause.

  • quantifiers

    Specifies the predicate quantifiers include ANY, SOME and ALL. ANY or SOME means if one of the patterns matches the input, then return true; ALL means if all the patterns matches the input, then return true.

Examples

  1. CREATE TABLE person (id INT, name STRING, age INT);
  2. INSERT INTO person VALUES
  3. (100, 'John', 30),
  4. (200, 'Mary', NULL),
  5. (300, 'Mike', 80),
  6. (400, 'Dan', 50),
  7. (500, 'Evan_w', 16);
  8. SELECT * FROM person WHERE name LIKE 'M%';
  9. +---+----+----+
  10. | id|name| age|
  11. +---+----+----+
  12. |300|Mike| 80|
  13. |200|Mary|null|
  14. +---+----+----+
  15. SELECT * FROM person WHERE name LIKE 'M_ry';
  16. +---+----+----+
  17. | id|name| age|
  18. +---+----+----+
  19. |200|Mary|null|
  20. +---+----+----+
  21. SELECT * FROM person WHERE name NOT LIKE 'M_ry';
  22. +---+------+---+
  23. | id| name|age|
  24. +---+------+---+
  25. |500|Evan_W| 16|
  26. |300| Mike| 80|
  27. |100| John| 30|
  28. |400| Dan| 50|
  29. +---+------+---+
  30. SELECT * FROM person WHERE name RLIKE 'M+';
  31. +---+----+----+
  32. | id|name| age|
  33. +---+----+----+
  34. |300|Mike| 80|
  35. |200|Mary|null|
  36. +---+----+----+
  37. SELECT * FROM person WHERE name REGEXP 'M+';
  38. +---+----+----+
  39. | id|name| age|
  40. +---+----+----+
  41. |300|Mike| 80|
  42. |200|Mary|null|
  43. +---+----+----+
  44. SELECT * FROM person WHERE name LIKE '%\_%';
  45. +---+------+---+
  46. | id| name|age|
  47. +---+------+---+
  48. |500|Evan_W| 16|
  49. +---+------+---+
  50. SELECT * FROM person WHERE name LIKE '%$_%' ESCAPE '$';
  51. +---+------+---+
  52. | id| name|age|
  53. +---+------+---+
  54. |500|Evan_W| 16|
  55. +---+------+---+
  56. SELECT * FROM person WHERE name LIKE ALL ('%an%', '%an');
  57. +---+----+----+
  58. | id|name| age|
  59. +---+----+----+
  60. |400| Dan| 50|
  61. +---+----+----+
  62. SELECT * FROM person WHERE name LIKE ANY ('%an%', '%an');
  63. +---+------+---+
  64. | id| name|age|
  65. +---+------+---+
  66. |400| Dan| 50|
  67. |500|Evan_W| 16|
  68. +---+------+---+
  69. SELECT * FROM person WHERE name LIKE SOME ('%an%', '%an');
  70. +---+------+---+
  71. | id| name|age|
  72. +---+------+---+
  73. |400| Dan| 50|
  74. |500|Evan_W| 16|
  75. +---+------+---+
  76. SELECT * FROM person WHERE name NOT LIKE ALL ('%an%', '%an');
  77. +---+----+----+
  78. | id|name| age|
  79. +---+----+----+
  80. |100|John| 30|
  81. |200|Mary|null|
  82. |300|Mike| 80|
  83. +---+----+----+
  84. SELECT * FROM person WHERE name NOT LIKE ANY ('%an%', '%an');
  85. +---+------+----+
  86. | id| name| age|
  87. +---+------+----+
  88. |100| John| 30|
  89. |200| Mary|null|
  90. |300| Mike| 80|
  91. |500|Evan_W| 16|
  92. +---+------+----+
  93. SELECT * FROM person WHERE name NOT LIKE SOME ('%an%', '%an');
  94. +---+------+----+
  95. | id| name| age|
  96. +---+------+----+
  97. |100| John| 30|
  98. |200| Mary|null|
  99. |300| Mike| 80|
  100. |500|Evan_W| 16|
  101. +---+------+----+