Testing String Equality or Relative Ordering : Char « Data Type « SQL / MySQL






Testing String Equality or Relative Ordering

      
mysql>
mysql>
mysql> CREATE TABLE mytable
    -> (
    ->  name    VARCHAR(20)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO mytable (name)
    ->  VALUES
    ->          ('copper'),
    ->          ('gold'),
    ->          ('iron'),
    ->          ('lead'),
    ->          ('mercury'),
    ->          ('platinum'),
    ->          ('silver'),
    ->          ('tin')
    -> ;
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT * FROM mytable;
+----------+
| name     |
+----------+
| copper   |
| gold     |
| iron     |
| lead     |
| mercury  |
| platinum |
| silver   |
| tin      |
+----------+
8 rows in set (0.00 sec)

mysql>
mysql>
mysql> SELECT name, name = 'lead', name != 'lead' FROM mytable;
+----------+---------------+----------------+
| name     | name = 'lead' | name != 'lead' |
+----------+---------------+----------------+
| copper   |             0 |              1 |
| gold     |             0 |              1 |
| iron     |             0 |              1 |
| lead     |             1 |              0 |
| mercury  |             0 |              1 |
| platinum |             0 |              1 |
| silver   |             0 |              1 |
| tin      |             0 |              1 |
+----------+---------------+----------------+
8 rows in set (0.00 sec)

mysql> SELECT name, name < 'lead', name > 'lead' FROM mytable;
+----------+---------------+---------------+
| name     | name < 'lead' | name > 'lead' |
+----------+---------------+---------------+
| copper   |             1 |             0 |
| gold     |             1 |             0 |
| iron     |             1 |             0 |
| lead     |             0 |             0 |
| mercury  |             0 |             1 |
| platinum |             0 |             1 |
| silver   |             0 |             1 |
| tin      |             0 |             1 |
+----------+---------------+---------------+
8 rows in set (0.00 sec)

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

   
    
    
    
    
    
  








Related examples in the same category

1.CHAR(n): character string with specified length
2.Quoting a string value
3.The CHAR data type is a fixed-length character data type that can store up to 255 characters.
4.Add zero to the values to force a string-to-number conversion
5.Writing Strings That Include Quotes or Special Characters
6.To include a quote character within a string that is quoted by the same kind of quote,
7.To find out whether a string lies within a given range of values (inclusive)
8.If you are comparing strings and numerics, or floating-point numbers and integers, MySQL will compare them as
9.Not equal char type value
10.Max with char type values
11.Compare char type value
12.Not null char type column