← Back to Chapters

MySQL LIKE Operator & Wildcards

? MySQL LIKE Operator & Wildcards

? Quick Overview

The LIKE operator in MySQL is used to search for a specified pattern in a column. It is commonly used in PHP-MySQL applications for flexible text searching using wildcards.

? Key Concepts

  • LIKE matches patterns instead of exact values
  • % represents multiple characters
  • _ represents a single character
  • NOT LIKE excludes matching patterns

? Syntax / Theory

? View Code Example
// Basic LIKE syntax used in SQL queries
SELECT column_name FROM table_name
WHERE column_name LIKE pattern;

? Code Examples

? Starts With ( % Wildcard )
// Select products starting with letter A
SELECT * FROM products
WHERE product_name LIKE 'A%';
? Single Character Match ( _ Wildcard )
// Select products with 'o' as the second character
SELECT * FROM products
WHERE product_name LIKE '_o%';
? Combined Wildcards
// Starts with A, ends with o, any one character in between
SELECT * FROM products
WHERE product_name LIKE 'A_%o';
? Excluding Results ( NOT LIKE )
// Exclude products starting with A
SELECT * FROM products
WHERE product_name NOT LIKE 'A%';

? Live Output / Explanation

These queries return filtered rows based on text patterns rather than exact matches, making them ideal for search features in PHP applications.

? Interactive Pattern Idea

You can dynamically build LIKE queries in PHP using user input from forms to implement live search functionality.

? Use Cases

  • Search boxes in PHP websites
  • Filtering products or users
  • Partial text matching
  • Autocomplete systems

? Tips & Best Practices

  • Use BINARY for case-sensitive searches
  • Avoid leading % for better performance
  • Escape user input in PHP to prevent SQL injection

? Try It Yourself

  • Find customers whose names contain son
  • List products ending with s
  • Exclude products starting with C