Syntax error Retrieve from MySQL only if it contains two hyphen symbols?

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)
Updated on: 2019-07-30T22:30:26+05:30

565 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements