Reading:  

Beginners guide to MySQL and MariaDB


MySQL LIKE clause

In MySQL LIKE condition allows wildcards to be used in WHERE clause of INSERT,SELECT, UPDATE, or DELETE statement. It allows to perform pattern matching.

Syntax :

expression LIKE pattern [ ESCAPE 'escape_character' ]

expression is character expression such as a column or field.

pattern is a character expression which contains pattern matching.

Wildcard

Explanation

%

Allows to match any string of any length (including zero length)

__

Allows to match on a single character

Here is an example

MySQL Like clause

From our PHP Script here is an example of using the LIKE clause

If we have to rewrite our SELECT query to only show Books from book_tbl WHERE author is C. Here is how we do it from a PHP script with PDO extension

<?php
$server = "localhost";
$user = "username";
$pwd = "password";

try {
      $connection = new PDO("mysql:host=$server;dbname=testDB", $user, $pwd);
      $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $sql = "select * from book_tbl where book_title LIKE 'Le%';";
// above will only select books for author C $select = $sql->prepare($sql); $select->execute(); $result = $select->fetchAll(); var_dump($result); } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } 

?>

 

In the next part we will see how to sort data on retreival. 

Description

In this tutorial, we will cover few topics that will give you a heads on start to build your knowledge on. Topics that we will cover briefly but still providing enough information are listed below

  • Overview
  • Installing on Linux and Windows
  • Some useful admin queries for starters
  • Connection
  • Create Database
  • Drop Database
  • Select Database
  • Data Type
  • Create Table
  • Drop Table
  • Inserting and Selecting data
  • Where Clause
  • Updating and deleting data
  • Like Clause
  • Sorting Result
  • Using Joins
  • Brief introduction to Regex, Transactions and Indexes
  • Alter Command
  • Temporary Tables
  • Database Info
  • Using Sequence
  • Database Export and Import
  • Resetting MySQL/MariaDB Administrator password


Audience

Absolute beginners looking to get a sneak peak into what MySQL. Please remember that this is not a full on guide but a quick introduction to the subject.

Learning Objectives

Get to know MySQL and MariaDB

Author: Subject Coach
Added on: 23rd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!