show records where the name has 6 characters : Introduction « Regular Expression « SQL / MySQL






show records where the name has 6 characters

      
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> # show records where the name has 6 characters
mysql> SELECT * FROM product WHERE name REGEXP "^......$";
+----+--------+-------+
| id | name   | price |
+----+--------+-------+
|  1 | Monaco |  6.99 |
|  3 | Mosaic |  6.99 |
+----+--------+-------+
2 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.Regular Expressions
2.Group the alternatives within parentheses, the ^ and $ will apply to both of them
3.The asterisk (*) indicates zero or more.
4.Limit the match on the 'i' to either zero or one.
5.As the plus sign (+) indicates that g had to appear one or more times
6.{3,} means the a must occur at least three times
7.MySQL's regular expression capabilities also support POSIX character classes.
8.Using Operators in Your SQL Statements
9.If you wish to encompass the entire character string, you must use ^ and $ in the search:
10.Name and regular expression
11.String with exact length
12.String case in regular expression
13.Where clause: regular expressions
14.Where clause: regular expression 2
15.A regular expression matches anywhere in the string.
16.Match only aaa
17.To match abcabcabc, you need to use parentheses
18.Regular expression and postcode
19.Regular expression and street value
20.Regular expression: or
21.Parentheses indicate an entire character string, and curly braces indicate how many times the character string
22.Square brackets indicate a selection from among several characters, a hyphen is used to indicate a range of ch