建表: CREATE TABLE tb_dept ( deptno INT PRIMARY KEY,#部门编号 dname VARCHAR(14),#部门名称 loc VARCHAR(13)#地址 ); REATE TABLE tb_emp ( empno INT PRIMARY KEY,#员工编号 ename VARCHAR(14),#员工姓名 job VARCHAR(10),#工作岗位 mgr INT,#部门经理 hiredate DATE,#入职日期 sal INT,#薪水 comm INT,#奖金 deptno INT#部门编号 ); 插入数据 INSERT INTO tb_dept VALUES (10,'CWB','BJ');INSERT INTO tb_dept VALUES (20,'YFB','GZ'); INSERT INTO tb_dept VALUES (30,'SCB','SH'); INSERT INTO tb_dept VALUES (40,'YWB','CD'); INSERT INTO tb_emp VALUES (7934,'linux','fenxi',7782,'1982-01-2',3000,300,20); INSERT INTO tb_emp VALUES (7935,'eddy','xiaoshou',8782,'1982-01-2',2000,200,10); INSERT INTO tb_emp VALUES (7936,'peter','yunwei',5782,'1982-01-2',4000,100,30); INSERT INTO tb_emp VALUES (7937,'oracle','wenyuan',6782,'1982-01-2',1000,500,40); INSERT INTO tb_emp VALUES (7938,'aaa','wenyuan',6782,'1983-04-2',2000,200,40); INSERT INTO tb_emp VALUES (7939,'yys','xiaoshou',8782,'1989-07-22',3000,600,10); INSERT INTO tb_emp VALUES (7940,'bbb','xiaoshou',8782,'1979-01-22',3000,NULL,10); INSERT INTO tb_emp VALUES (7941,'ccc','xiaoshou',8782,'1999-09-29',3000,0,10); 一、 #查找 字段、字段 从 表,*表示所有的列 mysql> SELECT deptno,dname,loc FROM tb_dept; +--------+-------+-----+ | deptno | dname | loc | +--------+-------+-----+ | 10 | CWB | BJ | | 20 | YFB | GZ | | 30 | SCB | SH | | 40 | YWB | CD | +--------+-------+-----+ 4 rows in set #查询所有字段 mysql> SELECT * FROM tb_dept; +--------+-------+-----+ | deptno | dname | loc | +--------+-------+-----+ | 10 | CWB | BJ | | 20 | YFB | GZ | | 30 | SCB | SH | | 40 | YWB | CD | +--------+-------+-----+ 4 rows in set mysql> #查询单个字段 SELECT dname FROM tb_dept; +-------+ | dname | +-------+ | CWB | | YFB | | SCB | | YWB | +-------+ 4 rows in set mysql> #算术表达式 sal*12表示年薪 SELECT ename,sal,sal*12 FROM tb_emp; +--------+------+--------+ | ename | sal | sal*12 | +--------+------+--------+ | linux | 3000 | 36000 | | eddy | 2000 | 24000 | | peter | 4000 | 48000 | | oracle | 1000 | 12000 | | aaa | 2000 | 24000 | | yys | 3000 | 36000 | | bbb | 3000 | 36000 | | ccc | 3000 | 36000 | +--------+------+--------+ 8 rows in set #可以加入括号等 mysql> SELECT ename,sal,sal*(12+100) FROM tb_emp; +--------+------+--------------+ | ename | sal | sal*(12+100) | +--------+------+--------------+ | linux | 3000 | 336000 | | eddy | 2000 | 224000 | | peter | 4000 | 448000 | | oracle | 1000 | 112000 | | aaa | 2000 | 224000 | | yys | 3000 | 336000 | | bbb | 3000 | 336000 | | ccc | 3000 | 336000 | +--------+------+--------------+ 8 rows in set mysql> #null和0还有空字符串不是一个概念 SELECT * FROM tb_emp; SELECT * FROM tb_emp WHERE comm = 0; SELECT * FROM tb_emp WHERE comm IS NULL; SELECT * FROM tb_emp WHERE comm IS NOT NULL; +-------+--------+----------+------+------------+------+------+--------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | +-------+--------+----------+------+------------+------+------+--------+ | 7934 | linux | fenxi | 7782 | 1982-01-02 | 3000 | 300 | 20 | | 7935 | eddy | xiaoshou | 8782 | 1982-01-02 | 2000 | 200 | 10 | | 7936 | peter | yunwei | 5782 | 1982-01-02 | 4000 | 100 | 30 | | 7937 | oracle | wenyuan | 6782 | 1982-01-02 | 1000 | 500 | 40 | | 7938 | aaa | wenyuan | 6782 | 1983-04-02 | 2000 | 200 | 40 | | 7939 | yys | xiaoshou | 8782 | 1989-07-22 | 3000 | 600 | 10 | | 7940 | bbb | xiaoshou | 8782 | 1979-01-22 | 3000 | NULL | 10 | | 7941 | ccc | xiaoshou | 8782 | 1999-09-29 | 3000 | 0 | 10 | +-------+--------+----------+------+------------+------+------+--------+ 8 rows in set +-------+-------+----------+------+------------+------+------+--------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | +-------+-------+----------+------+------------+------+------+--------+ | 7941 | ccc | xiaoshou | 8782 | 1999-09-29 | 3000 | 0 | 10 | +-------+-------+----------+------+------------+------+------+--------+ 1 row in set +-------+-------+----------+------+------------+------+------+--------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | +-------+-------+----------+------+------------+------+------+--------+ | 7940 | bbb | xiaoshou | 8782 | 1979-01-22 | 3000 | NULL | 10 | +-------+-------+----------+------+------------+------+------+--------+ 1 row in set +-------+--------+----------+------+------------+------+------+--------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | +-------+--------+----------+------+------------+------+------+--------+ | 7934 | linux | fenxi | 7782 | 1982-01-02 | 3000 | 300 | 20 | | 7935 | eddy | xiaoshou | 8782 | 1982-01-02 | 2000 | 200 | 10 | | 7936 | peter | yunwei | 5782 | 1982-01-02 | 4000 | 100 | 30 | | 7937 | oracle | wenyuan | 6782 | 1982-01-02 | 1000 | 500 | 40 | | 7938 | aaa | wenyuan | 6782 | 1983-04-02 | 2000 | 200 | 40 | | 7939 | yys | xiaoshou | 8782 | 1989-07-22 | 3000 | 600 | 10 | | 7941 | ccc | xiaoshou | 8782 | 1999-09-29 | 3000 | 0 | 10 | +-------+--------+----------+------+------------+------+------+--------+ 7 rows in set #null是指不可用,未分配的值 #空值不等于0或空格 #任意类型都支持空值 #包含空值的任何算数表达式都等于空 #字符串和null进行连接运算得到的也是null mysql> #定义字段别名 SELECT ename '姓名',sal '月薪',sal*12 '年薪' FROM tb_emp; #标准写法是要加上as SELECT ename AS "姓名" ,sal AS "月薪",sal*12 AS "年薪" FROM tb_emp; +--------+------+-------+ | 姓名 | 月薪 | 年薪 | +--------+------+-------+ | linux | 3000 | 36000 | | eddy | 2000 | 24000 | | peter | 4000 | 48000 | | oracle | 1000 | 12000 | | aaa | 2000 | 24000 | | yys | 3000 | 36000 | | bbb | 3000 | 36000 | | ccc | 3000 | 36000 | +--------+------+-------+ 8 rows in set mysql> #使用 DISTINCT可以从查询结果中清楚重复行 SELECT DISTINCT deptno FROM tb_emp; +--------+ | deptno | +--------+ | 20 | | 10 | | 30 | | 40 | +--------+ 4 rows in set #where比较运算符>,<,=,<>不等于,>=,<= SELECT * FROM tb_emp WHERE sal<>2000; #多个条件可以用and、or、not连接 SELECT *FROM tb_emp WHERE sal<1500 AND job='wenyuan'; SELECT *FROM tb_emp WHERE sal<1500 OR job='wenyuan'; SELECT * FROM tb_emp WHERE mgr NOT IN (7782,5782,6782); #.....and两个值之间,包含最小值和最大值 SELECT * FROM tb_emp where 1000 AND 3000 #IN匹配表示里面的值都要匹配 SELECT * FROM tb_emp WHERE mgr IN (7782,5782,6782); #not IN表示里面的值都不要匹配 SELECT * FROM tb_emp WHERE mgr NOT IN (7782,5782,6782); #使用like,%表示0个或多个字符,_表示一个字符 SELECT * FROM tb_emp WHERE ename LIKE '_a%'; #优先级规则查询job=wenyuan的全部查询出来,或者job=xiaoshou and sal>1500 SELECT * FROM tb_emp WHERE job='wenyuan' OR job='xiaoshou' AND sal>1500; #优先级规则查询job=wenyuan和job=xiaoshou全部查出 and sal>1500 SELECT * FROM tb_emp WHERE (job='wenyuan' OR job='xiaoshou') AND sal>1500; #对结果进行排序order by,desc降序排列 asc是升序默认的 SELECT * FROM tb_emp ORDER BY sal DESC; SELECT * FROM tb_emp ORDER BY sal ASC; 二、函数 /* concat 连接字符串 lower转换小写 upper转换大写 mysql> select ename from tb_emp; +--------+ | ename | +--------+ | linux | | eddy | | peter | | oracle | | aaa | | yys | | bbb | | ccc | +--------+ 8 rows in set mysql> select upper(ename) from tb_emp; +--------------+ | upper(ename) | +--------------+ | LINUX | | EDDY | | PETER | | ORACLE | | AAA | | YYS | | BBB | | CCC | +--------------+ 8 rows in set mysql> select lower(upper(ename)) from tb_emp; +---------------------+ | lower(upper(ename)) | +---------------------+ | linux | | eddy | | peter | | oracle | | aaa | | yys | | bbb | | ccc | +---------------------+ 8 rows in set length返回字符串长度 mysql> select length(ename) from tb_emp; +---------------+ | length(ename) | +---------------+ | 5 | | 4 | | 5 | | 6 | | 3 | | 3 | | 3 | | 3 | +---------------+ 8 rows in set 从substr(str,pos,len)查询str字段从pos位置起len个长度的字符默认是1 mysql> select substr(ename,1,2 ) from tb_emp; +-------------------+ | substr(ename,1,2) | +-------------------+ | li | | ed | | pe | | or | | aa | | yy | | bb | | cc | +-------------------+ 8 rows in set */ #连接字符串 SELECT CONCAT(dname,loc) FROM tb_dept; mysql> SELECT CONCAT(dname,loc) FROM tb_dept; +-------------------+ | CONCAT(dname,loc) | +-------------------+ | CWBBJ | | YFBGZ | | SCBSH | | YWBCD | +-------------------+ 4 rows in set #数值函数 #abs(x)返回x的绝对值 #ceil(x)返回不小于x的最小整数 #floor(x)返回不大于x的最大整数 #mod(x,y)返回x/y的模 #日期和时间的函数 #now返回当前时间 SELECT NOW(); #查询入职时间是1980年2月 SELECT * FROM tb_emp WHERE YEAR(hiredate) = 1982 AND MONTH(hiredate) = 2; #插入时间 INSERT INTO tb_emp (empno,ename,job,hiredate,sal) VALUES (8001,'jay','boss','2011-12-14',600); #插入时间now函数是有时分秒的但是date类型是会过滤掉的因为date是日期类型 INSERT INTO tb_emp (empno,ename,job,hiredate,sal) VALUES (8002,'join','boss',NOW(),600); #流程函数 /* case when [value1] then result1 value1是真就返回resul1 when [value2] then result2 value2是真就返回resul2 else default 否则返回default end if (expr1,expr2,expr3)如果expr1是true则if返回值expr2否则返回expr3 ifnull(expr1,expr2)如果expr1不为null,则返回expr1的值,否则返回值为expr2 */ #case when....then...else...and #条件判断 SELECT ename,job,sal,comm '原定奖金', CASE WHEN comm IS NULL THEN 100 ELSE comm END AS '奖金' FROM tb_emp; +--------+----------+------+----------+------+ | ename | job | sal | 原定奖金 | 奖金 | +--------+----------+------+----------+------+ | linux | fenxi | 3000 | 300 | 300 | | eddy | xiaoshou | 2000 | 200 | 200 | | peter | yunwei | 4000 | 100 | 100 | | oracle | wenyuan | 1000 | 500 | 500 | | aaa | wenyuan | 2000 | 200 | 200 | | yys | xiaoshou | 3000 | 600 | 600 | | bbb | xiaoshou | 3000 | NULL | 100 | | ccc | xiaoshou | 3000 | 0 | 0 | +--------+----------+------+----------+------+ 8 rows in set select e.ename,e.job,e.sal,d.dname, case when grade=1 then '第五等级' when grade=2 then '第四等级' when grade=3 then '第三等级' when grade=4 then '第二等级' when grade=5 then '第一等级' end as 工资等级 from scott.emp e join scott.dept d on (e.deptno=d.deptno) join scott.salgrade on (sal between losal and hisal); 把查询出来的1替换成第X等级 #ifnull,如果字段不为空则取第一个值,如果为空取第二个值 SELECT comm,IFNULL(comm,100) AS comm2 FROM tb_emp; mysql> SELECT comm,IFNULL(comm,100) AS comm2 FROM tb_emp; +------+-------+ | comm | comm2 | +------+-------+ | 300 | 300 | | 200 | 200 | | 100 | 100 | | 500 | 500 | | 200 | 200 | | 600 | 600 | | NULL | 100 | | 0 | 0 | +------+-------+ 8 rows in set #if,如果字段不为空或不为0则取第expr2的值,如果为空或为0取expr3的值 SELECT comm,IF(comm,comm+200,100) AS comm2 FROM tb_emp; mysql> SELECT comm,IF(comm,comm+200,100) AS comm2 FROM tb_emp; +------+-------+ | comm | comm2 | +------+-------+ | 300 | 500 | | 200 | 400 | | 100 | 300 | | 500 | 700 | | 200 | 400 | | 600 | 800 | | NULL | 100 | | 0 | 100 | +------+-------+ 8 rows in set