mysql -uroot -p Enter password: ****** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 5.7.28 MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database test -> ; Query OK, 1 row affected (0.00 sec) mysql> use test Database changed mysql> create table t1( -> id int auto_increment primary key, -> name varchar(255) not null, -> age int not null, -> salary int not null, -> work_desc varchar(255) -> ); Query OK, 0 rows affected (0.32 sec) mysql> insert into t1 values -> (1,'slk',18,0,null), -> (2,'echo',31,30000,'teacher'), -> (3,'tank',28,15000,'teacher'), -> (4,'egon',5,500,null); Query OK, 4 rows affected (0.14 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from t1; +----+------+-----+--------+-----------+ | id | name | age | salary | work_desc | +----+------+-----+--------+-----------+ | 1 | slk | 18 | 0 | NULL | | 2 | echo | 31 | 30000 | teacher | | 3 | tank | 28 | 15000 | teacher | | 4 | egon | 5 | 500 | NULL | +----+------+-----+--------+-----------+ 4 rows in set (0.00 sec)
--1. 查看岗位是teacher的员工姓名、年龄 mysql> select name, age -> from t1 where -> work_desc='teacher'; +------+-----+ | name | age | +------+-----+ | echo | 31 | | tank | 28 | +------+-----+ 2 rows in set (0.15 sec)
-- 2. 查看岗位是teacher且年龄大于30岁的员工姓名、年龄 mysql> select name, age -> from t1 where -> age > 30 and -> work_desc = 'teacher'; +------+-----+ | name | age | +------+-----+ | echo | 31 | +------+-----+ 1 row in set (0.03 sec)
-- 3. 查看岗位是teacher且薪资在9000-1000范围内的员工姓名、年龄、薪资 mysql> select name, age -> from t1 where -> work_desc = 'teacher' and -> salary between 9000 and 10000; Empty set (0.02 sec)
-- 4. 查看岗位描述不为NULL的员工信息 mysql> select * -> from t1 where -> work_desc != 'NULL'; +----+------+-----+--------+-----------+ | id | name | age | salary | work_desc | +----+------+-----+--------+-----------+ | 2 | echo | 31 | 30000 | teacher | | 3 | tank | 28 | 15000 | teacher | +----+------+-----+--------+-----------+ 2 rows in set (0.00 sec)
-- 5. 查看岗位是teacher且薪资是10000或9000或30000的员工姓名、年龄、薪资 mysql> select name, age, salary -> from t1 where -> work_desc = 'teacher' and -> salary in (10000,9000,30000); +------+-----+--------+ | name | age | salary | +------+-----+--------+ | echo | 31 | 30000 | +------+-----+--------+ 1 row in set (0.00 sec)
-- 6. 查看岗位是teacher且薪资不是10000或9000或30000的员工姓名、年龄、薪资 mysql> select name, age, salary -> from t1 where -> work_desc = 'teacher' and -> salary not in (10000,9000,30000); +------+-----+--------+ | name | age | salary | +------+-----+--------+ | tank | 28 | 15000 | +------+-----+--------+ 1 row in set (0.00 sec)
-- 7. 查看岗位是teacher且名字是jin开头的员工姓名、年薪 mysql> select name, age -> from t1 where -> name like 'jin%'; Empty set (0.03 sec)