{"id":28113,"date":"2023-01-06T14:18:33","date_gmt":"2023-01-06T22:18:33","guid":{"rendered":"https:\/\/coderpad.io\/?p=28113"},"modified":"2023-06-07T13:39:51","modified_gmt":"2023-06-07T20:39:51","slug":"mysql-like-operator-a-detailed-guide","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/mysql-like-operator-a-detailed-guide\/","title":{"rendered":"MySQL LIKE Operator: A Detailed Guide"},"content":{"rendered":"\n<p>A super common task when working with databases is searching for a text string&nbsp; matching a given pattern. You might need to get all customers whose last names start with a &#8220;B&#8221; or retrieve all products whose ids contain a certain numerical sequence. If MySQL is your database, then the MySQL <code>LIKE<\/code> operator is the solution you need.<\/p>\n\n\n\n<p>This post is a guide to using the <code>LIKE<\/code> operator in MySQL. We&#8217;ll explain what exactly the MySQL <code>LIKE<\/code> operator, and what you need it for and show several practical usage examples. Before we get started, let&#8217;s review some requirements for the post.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Prerequisites<\/strong><\/h2>\n\n\n\n<p>For this post, I assume you have some familiarity with the SQL language. You should be fine if you have experience with some relational database, even if it&#8217;s not MySQL.<\/p>\n\n\n\n<p>You&#8217;ll need a local MySQL instance running and a way to connect to that instance to run commands. I recommend using the<a href=\"https:\/\/hub.docker.com\/_\/mysql\" target=\"_blank\" rel=\"noopener\"> <\/a><a href=\"https:\/\/coderpad.io\/languages\/mysql\/\">CoderPad MySQL Sandbox<\/a> to quickly and easily get started writing SQL as it\u2019ll be your MySQL client for this article.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>MySQL LIKE operator: The what and why<\/strong><\/h2>\n\n\n\n<p>Let&#8217;s start with the basics. What is the <code>LIKE<\/code> operator in MySQL, and why would you need it?<\/p>\n\n\n\n<p>MySQL <code>LIKE<\/code> (and <code>like<\/code> in other database engines as well) is an SQL operator you use to match textual columns against certain patterns. You&#8217;ve already seen some examples in the introduction, but here&#8217;s a longer list of potential use cases for the <code>LIKE<\/code> operator:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Retrieving all phone numbers that start with a given country calling code<\/li>\n\n\n\n<li>Getting all email addresses that belong to a given domain<\/li>\n\n\n\n<li>Retrieving all users who live at a certain street or district<\/li>\n<\/ul>\n\n\n\n<p>In short, you&#8217;d typically use this operator when you need to retrieve potentially many rows that all have something in common, and such rows can&#8217;t be obtained through the use of the comparison operators (<code>&gt;, &lt;, ==, !=<\/code>, and so on) or clauses such as <code>BETWEEN<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>MySQL LIKE operator: some basic examples<\/strong><\/h2>\n\n\n\n<p>In your sandbox MySQL instance, run the following commands to create a database, select it and create a table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> students (<span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>, first_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">250<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>, last_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">250<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For the next steps, let&#8217;s add some students:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> students (first_name, last_name) <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Smith'<\/span>);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> students (first_name, last_name) <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'Pamelia'<\/span>, <span class=\"hljs-string\">'Black'<\/span>);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> students (first_name, last_name) <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'Jonathan'<\/span>, <span class=\"hljs-string\">'Grey'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now for the example. Let&#8217;s retrieve the <code>id<\/code> and <code>first_name<\/code> of students whose names start with &#8220;Jo&#8221;:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">id<\/span>, first_name <span class=\"hljs-keyword\">FROM<\/span> students <span class=\"hljs-keyword\">WHERE<\/span> first_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'Jo%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The command above results in the following output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">+<span class=\"hljs-comment\">----+------------+<\/span>\n| id | first_name |\n+<span class=\"hljs-comment\">----+------------+<\/span>\n|  1 | John       |\n|  2 | Jonathan   |\n+<span class=\"hljs-comment\">----+------------+<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Some things to note for this first example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>LIKE<\/code> operator is used alongside the <code>WHERE<\/code> clause, in the condition portion<\/li>\n\n\n\n<li>To filter for values that start with a given text, we specify the text followed by the <code>%<\/code> wildcard, which is a placeholder for &#8220;everything.&#8221;<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s now retrieve all students whose first name contains the letter &#8220;a&#8221;, in any position:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">id<\/span>, first_name <span class=\"hljs-keyword\">FROM<\/span> students <span class=\"hljs-keyword\">WHERE<\/span> first_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%a%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, as you can see, the wildcard is put before and after the letter. And here&#8217;s the result:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">+<span class=\"hljs-comment\">----+------------+<\/span>\n| id | first_name |\n+<span class=\"hljs-comment\">----+------------+<\/span>\n|  2 | Jonathan   |\n|  3 | Pamelia    |\n+<span class=\"hljs-comment\">----+------------+<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>That&#8217;s enough for you to get the gist of it. Let&#8217;s see more examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Matching a Single Character<\/strong><\/h2>\n\n\n\n<p>All of the examples you saw until now use the <code>%<\/code> character as a wildcard. This wildcard matches any number of characters. There&#8217;s another wildcard that matches exactly one character: the underscore (<code>_<\/code>) wildcard. Let&#8217;s see in practice how it works.<\/p>\n\n\n\n<p>Let&#8217;s insert two more students into the table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> students (first_name, last_name) <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'Joe'<\/span>, <span class=\"hljs-string\">'Martin'<\/span>);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> students (first_name, last_name) <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'Jon'<\/span>, <span class=\"hljs-string\">'Mendel'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Would it be possible to write a query to retrieve Joe and Jon, but not John or Jonathan? Yes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">id<\/span>, first_name <span class=\"hljs-keyword\">FROM<\/span> students <span class=\"hljs-keyword\">WHERE<\/span> first_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'Jo_'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The query above gets students whose first name starts with &#8220;Jo&#8221;, followed by exactly one character. Here&#8217;s the result:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">+<span class=\"hljs-comment\">----+------------+<\/span>\n| id | first_name |\n+<span class=\"hljs-comment\">----+------------+<\/span>\n|  4 | Joe        |\n|  5 | Jon        |\n+<span class=\"hljs-comment\">----+------------+<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\"><strong>A Negative Match<\/strong><\/h2>\n\n\n\n<p>What if you want to retrieve values that don&#8217;t match a given pattern? That&#8217;s also possible using the <code>NOT LIKE<\/code> clause. For instance, let&#8217;s say you want to retrieve students whose last names don&#8217;t start with an M. Here&#8217;s the query for that:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> students <span class=\"hljs-keyword\">WHERE<\/span> last_name <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'M%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The query above returns the students with last names &#8220;Black&#8221;, &#8220;Grey&#8221;, and &#8220;Smith&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Escaping The Wildcard<\/strong><\/h2>\n\n\n\n<p>What if the pattern you want to match contains the character &#8220;%&#8221; or &#8220;_&#8221;? In this case, it&#8217;s possible to escape the wildcard. Start by inserting a new student:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> students (first_name, last_name) <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'WeirdName%'<\/span>, <span class=\"hljs-string\">'_WeirderLastName'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, let&#8217;s try to match the last name:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> students <span class=\"hljs-keyword\">WHERE<\/span> last_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'_%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you ran the query above, you&#8217;ve seen that it gets all rows. Not what we wanted. Trying to match the first name will result in the same result.<\/p>\n\n\n\n<p>Fortunately, it&#8217;s easy to escape the offending character using a backslash, which is the default escaping character. Let&#8217;s rewrite the query above in the correct way:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">mysql&gt; <span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> students <span class=\"hljs-keyword\">WHERE<\/span> last_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'\\_%'<\/span>;\n\n+<span class=\"hljs-comment\">----+------------+------------------+<\/span>\n| id | first_name | last_name        |\n+<span class=\"hljs-comment\">----+------------+------------------+<\/span>\n|  6 | WeirdName% | _WeirderLastName |\n+<span class=\"hljs-comment\">----+------------+------------------+<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>I already include the results above. As you can see, adding the backslash tells MySQL to treat the underscore just like a normal character. Now, for the first name:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">mysql&gt; <span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> students <span class=\"hljs-keyword\">WHERE<\/span> first_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%\\%'<\/span>;\n\n+<span class=\"hljs-comment\">----+------------+------------------+<\/span>\n| id | first_name | last_name        |\n+<span class=\"hljs-comment\">----+------------+------------------+<\/span>\n|  6 | WeirdName% | _WeirderLastName |\n+<span class=\"hljs-comment\">----+------------+------------------+<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this case, the order is different. Since the student&#8217;s name ends with the percent character, first we include the wildcard and then the actual percentage character we&#8217;re trying to match, preceded by the escape character. As you can see above, it works like a charm.<\/p>\n\n\n\n<p>Finally, even though the backslash is the default escaping character, it&#8217;s not the only one. Actually, you can pick your own escaping character using the <code>ESCAPE<\/code> instruction. Let&#8217;s rewrite the query to retrieve match the last name, but specify the &#8220;#&#8221; character as the escape character:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> students <span class=\"hljs-keyword\">WHERE<\/span> last_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'#_%'<\/span> ESCAPE <span class=\"hljs-string\">'#'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And the result:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">+<span class=\"hljs-comment\">----+------------+------------------+<\/span>\n| id | first_name | last_name        |\n+<span class=\"hljs-comment\">----+------------+------------------+<\/span>\n|  6 | WeirdName% | _WeirderLastName |\n+<span class=\"hljs-comment\">----+------------+------------------+<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Test out the <code>LIKE<\/code> operator in the sandbox below:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 85%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=238708&#038;use_question_button\" width=\"640\" height=\"544\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this post, we&#8217;ve covered the MySQL LIKE operator. As you&#8217;ve seen, this operator can be used to retrieve values from textual columns which match a given pattern. We&#8217;ve walked you through several examples, in which you&#8217;ve learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>That there are two wildcards you can use with <code>LIKE<\/code>: the <code>%<\/code> character, which matches any number of characters, and the <code>_<\/code> character, which makes exactly one character<\/li>\n\n\n\n<li>It&#8217;s possible to escape the wildcards using the default escape character<\/li>\n\n\n\n<li>It&#8217;s also possible to specify a different escape character using the <code>ESCAPE<\/code> clauses<\/li>\n<\/ul>\n\n\n\n<p>Where should you go from now? Here are some suggestions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Learn about potential alternatives to <code>LIKE<\/code>, such as the <code>REGEXP<\/code> operator<\/li>\n\n\n\n<li>Research about how using <code>LIKE<\/code> on<a href=\"https:\/\/coderpad.io\/blog\/development\/how-to-use-indexes-to-increase-mysql-database-performance\/\"> indexed columns can affect query performance<\/a><\/li>\n\n\n\n<li>Try to learn about scenarios in which <code>LIKE<\/code> might not be the best option, such as when implementing a general search feature (full-text search) for your website<\/li>\n<\/ul>\n\n\n\n<p>Also, feel free to explore the CoderPad blog for more database-oriented posts, such as these:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/sql-functions-and-techniques-every-data-person-should-know\/\">SQL Functions and Techniques Every Data Person Should Know<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/optimize-mysql-database-schema\/\">Optimize MySQL Database Schema<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading, and until the next time.<\/p>\n\n\n\n<p><em>This post was written by Carlos Schults. <\/em><a href=\"https:\/\/carlosschults.net\" target=\"_blank\" rel=\"noopener\"><em>Carlos<\/em><\/a><em> is a consultant and software engineer with experience in desktop, web, and mobile development. Though his primary language is C#, he has experience with a number of languages and platforms. His main interests include automated testing, version control, and code quality.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A super common task when working with databases is searching for a text string\u00a0 matching a given pattern. This post is a guide to using the LIKE operator in MySQL. We&#8217;ll explain what exactly is MySQL like and what you need it for.<\/p>\n","protected":false},"author":1,"featured_media":28176,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[66],"keyword-cluster":[],"class_list":["post-28113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/28113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=28113"}],"version-history":[{"count":63,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/28113\/revisions"}],"predecessor-version":[{"id":34559,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/28113\/revisions\/34559"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/28176"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=28113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=28113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=28113"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=28113"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=28113"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=28113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}