
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Filter column value by the first character in MySQL
You can use LEFT() from MySQL. Let us first create a −
mysql> create table DemoTable1428 -> ( -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> EmployeeName varchar(20) -> ); Query OK, 0 rows affected (1.05 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1428(EmployeeName) values('Chris Brown'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1428(EmployeeName) values('Bob Brown'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1428(EmployeeName) values('John Smith'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1428(EmployeeName) values('David Miller'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1428(EmployeeName) values('John Doe'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1428(EmployeeName) values('Carol Johnson'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select −
mysql> select * from DemoTable1428;
This will produce the following output −
+------------+---------------+ | EmployeeId | EmployeeName | +------------+---------------+ | 1 | Chris Brown | | 2 | Bob Brown | | 3 | John Smith | | 4 | David Miller | | 5 | John Doe | | 6 | Carol Johnson | +------------+---------------+ 6 rows in set (0.00 sec)
Following is the query to filter column value by the first character −
mysql> select * from DemoTable1428 where left(EmployeeName,1)='J';
This will produce the following output −
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 3 | John Smith | | 5 | John Doe | +------------+--------------+ 2 rows in set (0.00 sec)
- Related Articles
- Get first date from timestamp in MySQL group by another column with duplicate value
- ORDER BY specific field value first in MySQL
- Set a specific value for the first three column values in MySQL?
- Find sum by removing first character from a string followed by numbers in MySQL?
- Fetch the first letter of a column value and insert it in another column with MySQL
- Group MySQL rows in an array by column value?
- Implement MySQL conditional GROUP BY with NOT IN to filter records from duplicate column values
- Filter query by current date in MySQL
- Set a certain value first with MySQL ORDER BY?
- How to cut only the first character in MySQL string?
- Sort a MySQL table column value by part of its value?
- Select all except the first character in a string in MySQL?
- Sort by character length in MySQL
- Remove all except the first character of a string in MySQL?
- Sort character values from a column mixed with character and numbers in MySQL?

Advertisements