Show records where the name contains a "W" or a "N" : Contains « Regular Expression « SQL / MySQL






Show records where the name contains a "W" or a "N"

      
mysql>
mysql> CREATE TABLE IF NOT EXISTS product
    -> (
    ->   id     INT             AUTO_INCREMENT PRIMARY KEY,
    ->   name   VARCHAR(25)     NOT NULL,
    ->   price DECIMAL(6,2)     NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> INSERT INTO product (name, price)   VALUES ("Monaco", 6.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO product (name, price)   VALUES ("Cavendish", 4.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO product (name, price)   VALUES ("Mosaic", 6.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO product (name, price)   VALUES ("Blue Reef", 8.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO product (name, price)   VALUES ("Silver Swirl", 14.99);
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> # show all data in the "product" table
mysql> SELECT * FROM product;
+----+--------------+-------+
| id | name         | price |
+----+--------------+-------+
|  1 | Monaco       |  6.99 |
|  2 | Cavendish    |  4.99 |
|  3 | Mosaic       |  6.99 |
|  4 | Blue Reef    |  8.99 |
|  5 | Silver Swirl | 14.99 |
+----+--------------+-------+
5 rows in set (0.00 sec)

mysql>
mysql>
mysql>
mysql> # show records where the name contains a "W" or a "N"
mysql> SELECT * FROM product WHERE name REGEXP "[WN]";
+----+--------------+-------+
| id | name         | price |
+----+--------------+-------+
|  1 | Monaco       |  6.99 |
|  2 | Cavendish    |  4.99 |
|  5 | Silver Swirl | 14.99 |
+----+--------------+-------+
3 rows in set (0.00 sec)

mysql>
mysql> drop table product;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
  








Related examples in the same category

1.String contains
2.Pattern match: string contains
3.Pattern match: contain substring
4.Show records where the name contains a "W"
5.Checking Whether a String Contains a Substring
6.Strings that contain a particular substring anywhere:
7.Strings that contain a substring at a specific position
8.Strings that contain a particular substring at any position:
9.Strings that contain a particular substring at a specific position: