Using Operators in Your SQL Statements : Introduction « Regular Expression « SQL / MySQL






Using Operators in Your SQL Statements

      
mysql>
mysql>
mysql> CREATE TABLE CDs
    -> (
    ->     CDID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->     CDName VARCHAR(50) NOT NULL,
    ->     InStock SMALLINT UNSIGNED NOT NULL,
    ->     OnOrder SMALLINT UNSIGNED NOT NULL,
    ->     Reserved SMALLINT UNSIGNED NOT NULL,
    ->     Department ENUM('Classical', 'Popular') NOT NULL,
    ->     Category VARCHAR(20)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> INSERT INTO CDs (CDName, InStock, OnOrder, Reserved, Department, Category) VALUES
    -> ('Xml', 10, 5, 3, 'Popular', 'Rock'),
    -> ('Java', 10, 5, 3, 'Classical', 'Opera'),
    -> ('SQL', 17, 4, 1, 'Popular', 'Jazz'),
    -> ('MySQL', 9, 4, 2, 'Classical', 'Dance'),
    -> ('CSS', 24, 2, 5, 'Classical', NULL),
    -> ('HTML', 16, 6, 8, 'Classical', NULL),
    -> ('Oracle', 2, 25, 6, 'Popular', 'Blues'),
    -> ('Javascript', 32, 3, 10, 'Popular', NULL),
    -> ('Data type', 12, 15, 13, 'Popular', 'Country'),
    -> ('Flash', 5, 20, 10, 'Popular', 'New Age'),
    -> ('Ajax', 24, 11, 14, 'Popular', 'New Age'),
    -> ('Photoshop', 42, 17, 17, 'Classical', NULL),
    -> ('Word', 25, 44, 28, 'Classical', 'Dance'),
    -> ('iPhone', 32, 15, 12, 'Classical', 'General'),
    -> ('MacBook', 20, 10, 5, 'Classical', 'Opera'),
    -> ('Linux', 23, 12, 8, 'Classical', 'General'),
    -> ('Shell', 23, 10, 17, 'Popular', 'Country'),
    -> ('Pascal', 18, 20, 10, 'Popular', 'Jazz'),
    -> ('Ruby', 22, 5, 7, 'Popular', 'Blues'),
    -> ('Sql Server', 28, 17, 16, 'Classical', 'General'),
    -> ('Opera', 10, 35, 12, 'Classical', 'Opera'),
    -> ('Safari', 15, 30, 14, 'Popular', NULL),
    -> ('C', 42, 0, 8, 'Popular', 'Blues'),
    -> ('C++', 16, 8, 8, 'Classical', 'General');
Query OK, 24 rows affected (0.00 sec)
Records: 24  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql>
mysql> SELECT CDName, InStock+OnOrder-Reserved AS Available
    -> FROM CDs
    -> WHERE CDName REGEXP '^[a-f]'
    -> ORDER BY CDName;
+-----------+-----------+
| CDName    | Available |
+-----------+-----------+
| Ajax      |        21 |
| C         |        34 |
| C++       |        16 |
| CSS       |        21 |
| Data type |        14 |
| Flash     |        15 |
+-----------+-----------+
6 rows in set (0.00 sec)

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

   
    
    
    
    
    
  








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.If you wish to encompass the entire character string, you must use ^ and $ in the search:
9.Name and regular expression
10.String with exact length
11.String case in regular expression
12.Where clause: regular expressions
13.Where clause: regular expression 2
14.show records where the name has 6 characters
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