- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to match one character in MySQL in place of %?
To match only one character in MySQL, you can use underscore(_) in place of %. The syntax is as follows:
SELECT *FROM yourTableName WHERE yourColumnName LIKE ‘yourString_’;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table OneCharactermatchDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> PassoutYear year, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.76 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into OneCharactermatchDemo(PassoutYear) values('2008'); Query OK, 1 row affected (0.14 sec) mysql> insert into OneCharactermatchDemo(PassoutYear) values('2007'); Query OK, 1 row affected (0.18 sec) mysql> insert into OneCharactermatchDemo(PassoutYear) values('2010'); Query OK, 1 row affected (0.10 sec) mysql> insert into OneCharactermatchDemo(PassoutYear) values('2011'); Query OK, 1 row affected (0.13 sec) mysql> insert into OneCharactermatchDemo(PassoutYear) values('2012'); Query OK, 1 row affected (0.12 sec) mysql> insert into OneCharactermatchDemo(PassoutYear) values('2006'); Query OK, 1 row affected (0.10 sec) mysql> insert into OneCharactermatchDemo(PassoutYear) values('2019'); Query OK, 1 row affected (0.14 sec) mysql> insert into OneCharactermatchDemo(PassoutYear) values('2016'); Query OK, 1 row affected (0.13 sec) mysql> insert into OneCharactermatchDemo(PassoutYear) values('2017'); Query OK, 1 row affected (0.34 sec) mysql> insert into OneCharactermatchDemo(PassoutYear) values('2015'); Query OK, 1 row affected (0.17 sec) mysql> insert into OneCharactermatchDemo(PassoutYear) values('2020'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from OneCharactermatchDemo;
The following is the output:
+----+-------------+ | Id | PassoutYear | +----+-------------+ | 1 | 2008 | | 2 | 2007 | | 3 | 2010 | | 4 | 2011 | | 5 | 2012 | | 6 | 2006 | | 7 | 2019 | | 8 | 2016 | | 9 | 2017 | | 10 | 2015 | | 11 | 2020 | +----+-------------+ 11 rows in set (0.00 sec)
Here is the query to match only one character using underscore. The query is as follows:
mysql> select *from OneCharactermatchDemo where PassoutYear Like '200_';
The following is the output:
+----+-------------+ | Id | PassoutYear | +----+-------------+ | 1 | 2008 | | 2 | 2007 | | 6 | 2006 | +----+-------------+ 3 rows in set (0.00 sec)
Advertisements