close[x]


MySQL

MySQL-Home MySQL-Environment setup MySQL- Workbench MySQL-Basic syntax MySQL-Operator MySQL-Data type MySQL-Comments MySQL-Create DB MySQL-Drop DB MySQL-Select DB MySQL-Create Table MySQL-Drop table MySQL-Truncate MySQL-Primary Key MySQL-Foreign Key MySQL-Null MySQL-Increment MySQL-Having MySQL-Top MySQL-Insert Statement MySQL-Select Statement MySQL-Alter Statement MySQL-Where MySQL-And & Or MySQL-Default values MySQL-Exists MySQL-Order by MySQL-View MySQL-Update Statement MySQL-Delete Statement MySQL-Like MySQL-Sort MySQL-Limit MySQL-Min MySQL-Max MySQL-Group MySQL-In MySQL-Between MySQL-Union MySQL-Count MySQL-Average MySQL-Sum MySQL-Date & Time MySQL-Import MySQL-Export MySQL-Index MySQL-Temporary MySQL-Join MySQL-Full Join MySQL-Inner Join MySQL-Left Join MySQL-Right Join MySQL-Store Procedure MySQL-Injection MySQL-PHP connection



learncodehere.com




MySQL - LIKE

MySQL LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

MySQL LIKE uses % and _..

  • % - The percent sign represents zero, one, or multiple characters
  • _ - The underscore represents a single character

  • Syntax : LIKE

    
    SELECT column1, column2, ...
    FROM table_name
    WHERE columnN LIKE pattern;   

    Let's put these statements into real use.

    We've a table named Students in our database that contains the following records:

    ROLL_NO NAME SUBJECT
    1 Will C++
    2 SAM Python
    3 Sara HTML
    4 Rim Java
    5 Micheal SQL
    6 Lara



    Now we will fetch records from the Students table with a NAME starting with "s".


    Example : LIKE

    
    SELECT * FROM Students
    WHERE NAME LIKE '%s';
    

    The above SELECT statement selects NAME starts with s alphabet Students table.

    Result

    ROLL_NO NAME SUBJECT
    2 SAM Python
    3 Sara HTML

    Examples showing different LIKE operators with '%' and '_' wildcards:

    Operator Description
    WHERE NAME LIKE 's%' Finds any values that start with "s"
    WHERE NAME LIKE '%s' Finds any values that end with "s"
    WHERE NAME LIKE '%or%' Finds any values that have "or" in any position
    WHERE NAME LIKE '_s%' Finds any values that have "s" in the second position
    WHERE NAME LIKE 's_%' Finds any values that start with "s" and are at least 2 characters in length
    WHERE NAME LIKE 's__%' Finds any values that start with "s" and are at least 3 characters in length
    WHERE NAME LIKE 's%o' Finds any values that start with "s" and ends with "o"