Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of LIKE in MySQL

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 262
    Comment on it

    This article explains how to use LIKE operator to get data from MySQL table based on pattern matching. The LIKE operator used with WHERE clause of SELECT, UPDATE and DELETE statement which search for a specified pattern in a column.


    In MySQL LIKE operator use two wildcard characters(%,_) for pattern matching.

    • % is used for matching of zero or more characters.
    • _ is used for matching of single character only.

    Example : Let suppose we have table 'student_marks' which contains the details of students for eg. id, name, marks etc.


    a. Get records of students whose name start with letter A

    <?php
     $query = "SELECT * FROM `student_marks` WHERE `name` LIKE 'A%' "
    ?>
    

    The above query return all records of students whose name start from letter A. for eg. Amit, Ajay etc.


    b. Get records of students whose name end with string 'it'

    <?php
     $query = "SELECT * FROM `student_marks` WHERE `name` LIKE '%it' "
    ?>
    

    The above query return records of all students whose name ending with string 'it'. for eg. Amit, Rohit etc.


    c. Get records of students whose name match with 'Amit' or contains the pattern 'Amit'

    <?php
     $query = "SELECT * FROM `student_marks` WHERE `name` LIKE '%Amit%' "
    ?>
    

    The above query will return the records of those students whose names are Amit.


    d. Get records of students with name 'A_it'

    <?php
     $query = "SELECT * FROM `student_marks` WHERE `name` LIKE 'A_it' "
    ?>
    

    (_) wilcard is used to match a single character. The above query returns the records with name like Amit.


 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: