Академический Документы
Профессиональный Документы
Культура Документы
use lab7db;
mysql>
//before distinct
mysql> select lastname from employees;
+----------+
| lastname |
+----------+
| Osupile |
| Frecky |
| Maroba |
| Frecky |
+----------+
4 rows in set (0.01 sec)
//after distinct, the redundant will not be displayed more than one time
mysql> select distinct lastname from employees;
+----------+
| lastname |
+----------+
| Osupile |
| Frecky |
| Maroba |
+----------+
3 rows in set (0.02 sec)
//where equal
mysql> select * from employees where country ='Brunei';
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.00 sec)
// and operator
mysql> select dob from employees where firstname ='elsie' and lastname ='frecky'
;
+------------+
| dob |
+------------+
| 1980-10-20 |
+------------+
1 row in set (0.00 sec)
//or operator
mysql> select dob from employees where firstname ='elsie' or lastname ='frecky';
+------------+
| dob |
+------------+
| 1974-04-04 |
| 1980-10-20 |
+------------+
2 rows in set (0.00 sec)
//and or operator
mysql> select dob from employees where (firstname ='elsie' or firstname = 'steve
n') and lastname ='frecky';
+------------+
| dob |
+------------+
| 1974-04-04 |
| 1980-10-20 |
+------------+
2 rows in set (0.00 sec)
//before sorting
mysql> select firstname from employees;
+-----------+
| firstname |
+-----------+
| John |
| Steven |
| Agang |
| Elsie |
+-----------+
4 rows in set (0.00 sec)
//same as above
mysql> select * from employees limit 0,3;
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-
6745441 | Nigeria | 2 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.00 sec)
//starts with anything and ends with anything and the middle will has n
mysql> select * from employees where firstname like '%n%';
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-
6745441 | Nigeria | 2 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.01 sec)
// use or
mysql> select * from employees where country = 'brunei' or country = 'nigeria';
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-
6745441 | Nigeria | 2 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
4 rows in set (0.00 sec)
//use and
mysql> select * from employees where children >=3 and children <=5;
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.02 sec)
//another way use between, shorter code. it includes the 3 and 5 as well
mysql> select * from employees where children between 3 and 5;
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.00 sec)
//before alias
mysql> select firstname, children from employees;
+-----------+----------+
| firstname | children |
+-----------+----------+
| John | 5 |
| Steven | 2 |
| Agang | 3 |
| Elsie | 4 |
+-----------+----------+
4 rows in set (0.00 sec)