Compare String in where clause : String Compare « String « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » String » String Compare 
Compare String in where clause
  
/*
mysql> select * from employee_person;
+----+-------------------------------------+---------+---------------------------+------------+------+----------+-----------------+----------+
| id | address                             | phone   | email | birthday   | sex  | m_status | s_name          | children |
+----+-------------------------------------+---------+---------------------------+------------+------+----------+-----------------+----------+
|  1 | 200, Regina Street                  | 7176666 | [email protected] | 1971-04-26 | M    | Y        | Ane Regina      |     NULL |
|  2 | 1232 Alberta Road                   | 5553312 | [email protected] | 1968-03-02 | M    | Y        | Jane Van        |        3 |
|  3 | 90 Potter A                         | 3331211 | [email protected] | 1967-09-22 | M    | N        | Sandhya Pil     |        2 |
|  4 | 202, Donna Street                   | 7176167 | [email protected] | 1976-08-09 | F    | Y        | Manish Sha      |     NULL |
|  5 | Apartment #8, UBC, Van Street       | 8973242 | [email protected] | 1974-10-14 | F    | N        | NULL            |     NULL |
|  6 | 46 SFU Street                       | 6451234 | [email protected] | 1978-12-31 | M    | N        | NULL            |     NULL |
|  7 | 432 Mercury Ave                     | 7932232 | [email protected] | 1966-08-21 | M    | Y        | Mary Shelly     |        3 |
|  8 | 8 Little YaleTown                   | 5442994 | [email protected] | 1975-01-14 | M    | N        | NULL            |     NULL |
|  9 | 64 Temp Road                        | 4327652 | [email protected] | 1969-05-19 | M    | Y        | Man Nanda       |        1 |
| 10 | 132 Metro House, Henry Street       | 5552376 | [email protected] | 1968-07-06 | M    | N        | NULL            |     NULL |
| 11 | 1 Grace Town, Van Avenue            | 5433879 | [email protected] | 1957-11-04 | M    | Y        | Muriel Lovelace |        4 |
| 12 | 97 Oakland Road                     | 5423311 | [email protected] | 1968-02-15 | M    | Y        | Rina Brighton   |        3 |
| 13 | 543 Applegate Lane                  | 3434343 | [email protected] | 1968-09-03 | F    | Y        | Matt Shi        |        2 |
| 14 | 76 Fish Street                      | 7432433 | [email protected] | 1965-04-28 | M    | N        | NULL            |     NULL |
| 15 | 98 Gun Street                       | 6500787 | [email protected] | 1966-06-23 | M    | Y        | Betty Cudly     |        3 |
| 16 | #5 Winnepag Homes                   | 5433243 | [email protected] | 1964-03-06 | M    | Y        | Stella Stevens  |        2 |
| 17 | 652 Devon Building, 6th Jade Avenue | 5537885 | [email protected] | 1970-04-18 | F    | Y        | Edgar Alan      |        1 |
| 18 | Apartment #9, Together Towers       | 5476565 | [email protected] | 1973-10-09 | M    | N        | NULL            |     NULL |
| 19 | Apartment #9, West Towers           | 5476565 | [email protected] | 1973-01-20 | M    | N        | NULL            |     NULL |
| 20 | 90 Yale Town                        | 7528326 | [email protected] | 1968-01-25 | F    | N        | NULL            |     NULL |
| 21 | 4329 Eucalyptus Avenue              | 4254863 | [email protected] | 1964-06-13 | M    | Y        | Ruby Richer     |        2 |
+----+-------------------------------------+---------+---------------------------+------------+------+----------+-----------------+----------+
21 rows in set (0.00 sec)

mysql> update employee SET
    ->        title = 'Web Developer'
    ->        WHERE title = 'Web Designer';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from employee_person;
+----+-------------------------------------+---------+---------------------------+------------+------+----------+-----------------+----------+
| id | address                             | phone   | email | birthday   | sex  | m_status | s_name          | children |
+----+-------------------------------------+---------+---------------------------+------------+------+----------+-----------------+----------+
|  1 | 200, Regina Street                  | 7176666 | [email protected] | 1971-04-26 | M    | Y        | Ane Regina      |     NULL |
|  2 | 1232 Alberta Road                   | 5553312 | [email protected] | 1968-03-02 | M    | Y        | Jane Van        |        3 |
|  3 | 90 Potter A                         | 3331211 | [email protected] | 1967-09-22 | M    | N        | Sandhya Pil     |        2 |
|  4 | 202, Donna Street                   | 7176167 | [email protected] | 1976-08-09 | F    | Y        | Manish Sha      |     NULL |
|  5 | Apartment #8, UBC, Van Street       | 8973242 | [email protected] | 1974-10-14 | F    | N        | NULL            |     NULL |
|  6 | 46 SFU Street                       | 6451234 | [email protected] | 1978-12-31 | M    | N        | NULL            |     NULL |
|  7 | 432 Mercury Ave                     | 7932232 | [email protected] | 1966-08-21 | M    | Y        | Mary Shelly     |        3 |
|  8 | 8 Little YaleTown                   | 5442994 | [email protected] | 1975-01-14 | M    | N        | NULL            |     NULL |
|  9 | 64 Temp Road                        | 4327652 | [email protected] | 1969-05-19 | M    | Y        | Man Nanda       |        1 |
| 10 | 132 Metro House, Henry Street       | 5552376 | [email protected] | 1968-07-06 | M    | N        | NULL            |     NULL |
| 11 | 1 Grace Town, Van Avenue            | 5433879 | [email protected] | 1957-11-04 | M    | Y        | Muriel Lovelace |        4 |
| 12 | 97 Oakland Road                     | 5423311 | [email protected] | 1968-02-15 | M    | Y        | Rina Brighton   |        3 |
| 13 | 543 Applegate Lane                  | 3434343 | [email protected] | 1968-09-03 | F    | Y        | Matt Shi        |        2 |
| 14 | 76 Fish Street                      | 7432433 | [email protected] | 1965-04-28 | M    | N        | NULL            |     NULL |
| 15 | 98 Gun Street                       | 6500787 | [email protected] | 1966-06-23 | M    | Y        | Betty Cudly     |        3 |
| 16 | #5 Winnepag Homes                   | 5433243 | [email protected] | 1964-03-06 | M    | Y        | Stella Stevens  |        2 |
| 17 | 652 Devon Building, 6th Jade Avenue | 5537885 | [email protected] | 1970-04-18 | F    | Y        | Edgar Alan      |        1 |
| 18 | Apartment #9, Together Towers       | 5476565 | [email protected] | 1973-10-09 | M    | N        | NULL            |     NULL |
| 19 | Apartment #9, West Towers           | 5476565 | [email protected] | 1973-01-20 | M    | N        | NULL            |     NULL |
| 20 | 90 Yale Town                        | 7528326 | [email protected] | 1968-01-25 | F    | N        | NULL            |     NULL |
| 21 | 4329 Eucalyptus Avenue              | 4254863 | [email protected] | 1964-06-13 | M    | Y        | Ruby Richer     |        2 |
+----+-------------------------------------+---------+---------------------------+------------+------+----------+-----------------+----------+
21 rows in set (0.00 sec)

*/
Drop table employee;

CREATE TABLE employee (
    id int unsigned not null auto_increment primary key,
    firstname varchar(20),
    lastname varchar(20),
    title varchar(30),
    age int,
    yearofservice int,
    salary int,
    perks int,
    email varchar(60)
)



INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("John""Chen""Senior Programmer"31312000025000"[email protected]");

INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Jan""Pillai""Senior Programmer"32411000020000"[email protected]");

INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Ane""Pandit""Web Designer"2439000015000"[email protected]");

INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Mary""Anchor""Web Designer"2728500015000"[email protected]");

INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Fred""King""Programmer"3237500015000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("John""Mac""Programmer"3248000016000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Arthur""Sam""Programmer"2827500014000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Alok""Nanda""Programmer"3237000010000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Susan""Ra""Multimedia Programmer"3249000015000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Paul""Simon""Multimedia Programmer"2318500012000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Edward""Parhar""Multimedia Programmer"3027500015000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Kim""Hunter""Senior Web Designer"32411000020000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Roger""Lewis""System Administrator"32310000013000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Danny""Gibson""System Administrator"3129000012000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Mike""Harper""Senior Marketing Executive"36112000028000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Mary""Sunday""Marketing Executive"3159000025000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Jack""Sim""Marketing Executive"2717000018000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Joe""Irvine""Marketing Executive"2717200018000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Henry""Ali""Customer Service Manager"323700009000"[email protected]");
INSERT INTO employee (firstname, lastName, title, age, yearofservice, salary, perks, emailvalues ("Peter""Champion""Finance Manager"32212000025000"[email protected]");

select from employee_person;


update employee SET
       title = 'Web Developer'
       WHERE title = 'Web Designer';
       
select from employee_person;       
           
         
    
  
Related examples in the same category
1.String comparisons are performed from left to right, one character at a time:
2.The two strings '4200' and '4200.0' are not the same:
3.Case-insensitive nature of string comparisons:
4.String comparisons in MySQL are not case sensitive by default:
5.To make a string comparison case sensitive, cast (convert) one of the strings to binary form by using the BINA
6.To make a string comparison not case sensitive, convert both strings to the same lettercase using UPPER( ) or
7.NULL values fail comparisons both with < and with >=
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.