MySQL学习之单表查询

Posted ITWEL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL学习之单表查询相关的知识,希望对你有一定的参考价值。

MySQL学习之单表查询_单表查询

一.单表查询

(一). 查询字段

I.单个字段

格式:      select 字段名 from 表名;

说明:

① select 和from都是关键字

② 字段名和表名都是标识符

③ SQL语句不区分大小写,并且都是以“ ;”结尾

演示:

mysql> desc dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| DEPTNO | int | NO | PRI | NULL | |
| DNAME | varchar(14) | YES | | NULL | |
| LOC | varchar(13) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select dname from dept;
+------------+
| dname |
+------------+
| ACCOUNTING |
| RESEARCH |
| SALES |
| OPERATIONS |
+------------+
4 rows in set (0.00 sec)

II.查询多个字段

方法:     在I的基础上通过“ ,”将多个字段名隔开即可

演示:

mysql> select dname,deptno from dept;
+------------+--------+
| dname | deptno |
+------------+--------+
| ACCOUNTING | 10 |
| RESEARCH | 20 |
| SALES | 30 |
| OPERATIONS | 40 |
+------------+--------+
4 rows in set (0.00 sec)

III.查询所有字段

格式:

    (1) select 字段一,字段二,字段三 ... from tablename;      (所有字段)

    (2)select * from tablename;

说明:

方式(2)特点:

                   ①效率低(需要先将 * 转化为 字段 ,然后再执行 )

                   ②可读性差

应用场景:需要快速查看表中所有数据时可以使用

演示:

方式一:

mysql> select dname,deptno,loc from dept;
+------------+--------+----------+
| dname | deptno | loc |
+------------+--------+----------+
| ACCOUNTING | 10 | NEW YORK |
| RESEARCH | 20 | DALLAS |
| SALES | 30 | CHICAGO |
| OPERATIONS | 40 | BOSTON |
+------------+--------+----------+
4 rows in set (0.00 sec)

方式二:

mysql> select* from dept;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
4 rows in set (0.00 sec)

(二). 列起别名

           方法:通过使用as 关键字来实现的

说明:

①  使用as 关键字起别名只是将结果列名显示为别名,原表列名是不变的

②  select 语句永远不会进行修改操作的(只负责查询)

演示:

方式一(不省略关键字as):

mysql> select* from dept;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
4 rows in set (0.00 sec)

mysql> select deptno,dname as deptname from dept; #deptname为dname的别名
+--------+------------+
| deptno | deptname |
+--------+------------+
| 10 | ACCOUNTING |
| 20 | RESEARCH |
| 30 | SALES |
| 40 | OPERATIONS |
+--------+------------+
4 rows in set (0.00 sec)

方式二(省略关键字as):

mysql> select deptno,dname  deptname from dept;
+--------+------------+
| deptno | deptname |
+--------+------------+
| 10 | ACCOUNTING |
| 20 | RESEARCH |
| 30 | SALES |
| 40 | OPERATIONS |
+--------+------------+
4 rows in set (0.00 sec)

mysql> select deptno,dname deptname name from dept;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near name from dept at line 1

补充,如果别名中存在空格,可以将这整个别名使用   ‘’  或  “ ” 包括起来  

mysql> select deptno,dname  deptname name from dept;
+--------+---------------+
| deptno | deptname name |
+--------+---------------+
| 10 | ACCOUNTING |
| 20 | RESEARCH |
| 30 | SALES |
| 40 | OPERATIONS |
+--------+---------------+
4 rows in set (0.00 sec)

注意: 

     ① 在所有的数据库当中,字符串统一使用单引号(标准)括起来

     ②双引号在oracle数据库中不能使用,但是在mysql数据库中是可以使用的

字段可以使用数学表达式:

mysql> select ename,sal*12 as yearsal from emp;
+--------+----------+
| ename | yearsal |
+--------+----------+
| SMITH | 9600.00 |
| ALLEN | 19200.00 |
| WARD | 15000.00 |
| JONES | 35700.00 |
| MARTIN | 15000.00 |
| BLAKE | 34200.00 |
| CLARK | 29400.00 |
| SCOTT | 36000.00 |
| KING | 60000.00 |
| TURNER | 18000.00 |
| ADAMS | 13200.00 |
| JAMES | 11400.00 |
| FORD | 36000.00 |
| MILLER | 15600.00 |
+--------+----------+
14 rows in set (0.00 sec)

       说明:如果 别名 为中文,需要使用 单引号     ‘  ’   括起来

(三) 条件查询

介绍:根据条件查询出符合要求的。

语法格式:

    select   字段1,字段2,字段3  ...from    tablename  where 条件;

常见条件如下表:        

运算符

说明

   =

等于

    <> 或!=

不等于

   <

小于

   <=

小于等于

  >

大于

   >=

大于等于

between… and ….

两个值之间,等同于 **>= and <=**

is null

为 null(is not null 不为空)

and

并且

or

或者

in

包含,相当于多个 o(r not in 不在这个范围中)

not

not 可以取非,主要用在 is 或 in 中

like

like 称为模糊查询,支持 % 或下划线匹配 % 匹配任意个字符 下划线,一个下划线只匹配一个字符

应用实例:

(1)=  (查询薪资等于 1600的员工姓名和编号

mysql> select empno,ename  from emp where sal=1600 ;
+-------+-------+
| empno | ename |
+-------+-------+
| 7499 | ALLEN |
+-------+-------+
1 row in set (0.01 sec)

(2)!=查询薪资不等于 1600的员工姓名和编号

mysql> select empno,ename  from emp where sal!=1600 ;
+-------+--------+
| empno | ename |
+-------+--------+
| 7369 | SMITH |
| 7521 | WARD |
| 7566 | JONES |
| 7654 | MARTIN |
| 7698 | BLAKE |
| 7782 | CLARK |
| 7788 | SCOTT |
| 7839 | KING |
| 7844 | TURNER |
| 7876 | ADAMS |
| 7900 | JAMES |
| 7902 | FORD |
| 7934 | MILLER |
+-------+--------+
13 rows in set (0.00 sec)

(3) <   (查询薪资小于1600的员工姓名和编号

mysql> select empno,ename,sal  from emp where sal<1600 ;
+-------+--------+---------+
| empno | ename | sal |
+-------+--------+---------+
| 7369 | SMITH | 800.00 |
| 7521 | WARD | 1250.00 |
| 7654 | MARTIN | 1250.00 |
| 7844 | TURNER | 1500.00 |
| 7876 | ADAMS | 1100.00 |
| 7900 | JAMES | 950.00 |
| 7934 | MILLER | 1300.00 |
+-------+--------+---------+
7 rows in set (0.00 sec)

(4) = (查询SMITH的编号和薪资)

mysql> select empno,ename,sal  from emp where ename=SMITH ;
+-------+-------+--------+
| empno | ename | sal |
+-------+-------+--------+
| 7369 | SMITH | 800.00 |
+-------+-------+--------+
1 row in set (0.00 sec)

说明:字符串使用单引号

(5)betweent ... and  ... (查询薪资在2450到 4000之间的员工姓名、编号、薪资)

mysql> select empno,ename,sal  from emp where sal between 2450 and 4000 ;
+-------+-------+---------+
| empno | ename | sal |
+-------+-------+---------+
| 7566 | JONES | 2975.00 |
| 7698 | BLAKE | 2850.00 |
| 7782 | CLARK | 2450.00 |
| 7788 | SCOTT | 3000.00 |
| 7902 | FORD | 3000.00 |
+-------+-------+---------

说明:必须遵循左小右大原则

(6)is null  (查询哪些员工的津贴/补助为null)

mysql> select empno,ename,sal,comm  from emp where comm is null;
+-------+--------+---------+------+
| empno | ename | sal | comm |
+-------+--------+---------+------+
| 7369 | SMITH | 800.00 | NULL |
| 7566 | JONES | 2975.00 | NULL |
| 7698 | BLAKE | 2850.00 | NULL |
| 7782 | CLARK | 2450.00 | NULL |
| 7788 | SCOTT | 3000.00 | NULL |
| 7839 | KING | 5000.00 | NULL |
| 7876 | ADAMS | 1100.00 | NULL |
| 7900 | JAMES | 950.00 | NULL |
| 7902 | FORD | 3000.00 | NULL |
| 7934 | MILLER | 1300.00 | NULL |
+-------+--------+---------+------+
10 rows in set (0.00 sec)

注意:在数据库当中null不能使用等号来进行衡量。需要使用is null,因为数据库中的null代表什么也没有,它不是一个值,所以不能使用等号衡量。


如果查询员工的津贴不是null,通过如下命令:

mysql>select empno ,ename,sal,comm from emp where  comm is not null;

mysql> select empno ,ename,sal,comm from emp where  comm is not null;
+-------+--------+---------+---------+
| empno | ename | sal | comm |
+-------+--------+---------+---------+
| 7499 | ALLEN | 1600.00 | 300.00 |
| 7521 | WARD | 1250.00 | 500.00 |
| 7654 | MARTIN | 1250.00 | 1400.00 |
| 7844 | TURNER | 1500.00 | 0.00 |
+-------+--------+---------+---------+
4 rows in set (0.00 sec)

(7) and   并且(查询工作岗位是MANAGER并且工资大于2500的员工信息

mysql> select empno,ename,job,sal from emp where job =MANAGER and sal > 2500;
+-------+-------+---------+---------+
| empno | ename | job | sal |
+-------+-------+---------+---------+
| 7566 | JONES | MANAGER | 2975.00 |
| 7698 | BLAKE | MANAGER | 2850.00 |
+-------+-------+---------+---------+
2 rows in set (0.00 sec)

(8)or 或则  (查询岗位是MANAGER和 SALESMAN的员工

mysql> select empno,ename,job from emp where job = MANAGER or job = SALESMAN;
+-------+--------+----------+
| empno | ename | job |
+-------+--------+----------+
| 7499 | ALLEN | SALESMAN |
| 7521 | WARD | SALESMAN |
| 7566 | JONES | MANAGER |
| 7654 | MARTIN | SALESMAN |
| 7698 | BLAKE | MANAGER |
| 7782 | CLARK | MANAGER |
| 7844 | TURNER | SALESMAN |
+-------+--------+----------+
7 rows in set (0.00 sec)

混合应用:

查询工资大于2500,并且部门编号为10或20部门的员工

#错误写法:and的优先级高于or(原因)
mysql> select * from emp where sal 以上是关于MySQL学习之单表查询的主要内容,如果未能解决你的问题,请参考以下文章

MySQL学习之视图的使用

Hive学习之排序

mysql_单表查询

mysql超哥

oracle学习之多表查询,子查询以及事务处理

mysql 数据操作 单表查询 查询排序: order by