- 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
Retrieve from MySQL only if it contains two hyphen symbols?
For this, use the LIKE operator. Let us first create a table:
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Password varchar(100) ); Query OK, 0 rows affected (1.27 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Password) values('John@--123'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(Password) values('---Carol234'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Password) values('--David987'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Password) values('Mike----53443'); Query OK, 1 row affected (0.30 sec)
Display all records from the table using select statement:
mysql> select *from DemoTable;
Output
+----+---------------+ | Id | Password | +----+---------------+ | 1 | John@--123 | | 2 | ---Carol234 | | 3 | --David987 | | 4 | Mike----53443 | +----+---------------+ 4 rows in set (0.00 sec)
Following is the query to retrieve from MySQL if it only contains two hyphen symbols -
mysql> select *from DemoTable where Password like '%--%' and password not like '%---%';
Output
+----+------------+ | Id | Password | +----+------------+ | 1 | John@--123 | | 3 | --David987 | +----+------------+ 2 rows in set (0.06 sec)
Advertisements