Comparison operators 2 : Operator « Select Clause « SQL / MySQL






Comparison operators 2

      
mysql>
mysql> CREATE TABLE IF NOT EXISTS product
    -> (
    ->   code           CHAR(8)         PRIMARY KEY,
    ->   make           VARCHAR(25)     NOT NULL,
    ->   model          VARCHAR(25)     NOT NULL,
    ->   price          DECIMAL(3,2)    NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> # insert 5 records into the "product" table
mysql> INSERT INTO product (code, make, model, price)   VALUES ("512/4792", "Alba", "C2108", 6.75);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO product (code, make, model, price)   VALUES ("512/4125", "Hitachi", "KC30", 8.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO product (code, make, model, price)   VALUES ("512/1458", "Philips", "AJ3010", 19.99);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> INSERT INTO product (code, make, model, price)   VALUES ("512/3669", "Morphy Richards", "28025", 19.99);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> INSERT INTO product (code, make, model, price)   VALUES ("512/1444", "Sony", "C253", 29.49);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql>
mysql> # show records in "product" if price is below 19.99
mysql> SELECT * FROM product WHERE price < 19.99;
+----------+-----------------+--------+-------+
| code     | make            | model  | price |
+----------+-----------------+--------+-------+
| 512/4792 | Alba            | C2108  |  6.75 |
| 512/4125 | Hitachi         | KC30   |  8.99 |
| 512/1458 | Philips         | AJ3010 |  9.99 |
| 512/3669 | Morphy Richards | 28025  |  9.99 |
| 512/1444 | Sony            | C253   |  9.99 |
+----------+-----------------+--------+-------+
5 rows in set (0.00 sec)

mysql>
mysql> # show records in "product" if price is above 19.99
mysql> SELECT * FROM product WHERE price > 19.99;
Empty set (0.00 sec)

mysql>
mysql> # show records in "product" if price is 19.99 or less
mysql> SELECT * FROM product WHERE price <= 19.99;
+----------+-----------------+--------+-------+
| code     | make            | model  | price |
+----------+-----------------+--------+-------+
| 512/4792 | Alba            | C2108  |  6.75 |
| 512/4125 | Hitachi         | KC30   |  8.99 |
| 512/1458 | Philips         | AJ3010 |  9.99 |
| 512/3669 | Morphy Richards | 28025  |  9.99 |
| 512/1444 | Sony            | C253   |  9.99 |
+----------+-----------------+--------+-------+
5 rows in set (0.00 sec)

mysql>
mysql> # delete this sample table
mysql> DROP TABLE IF EXISTS product;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
  








Related examples in the same category

1.Comparison Operators
2.Arithmetic Operators
3.Sort Operators
4.special <=> operator, which is like = except that it works with NULL operands by treating them as any other va
5.Compare NULL with <=> operator
6.Not operator
7.or operator vs in operator
8.Comparison Operators and null
9.Use the basic mathematical operators
10.In operator and a long list of integer
11.Less than and equals(<=) operators with All operator
12.Reversing or Negating Query Conditions
13.Not equal