目录
因为记了一个星期的笔记没保存,所以这次。。。就随便糊弄糊弄吧。。。
表和数据的 创建 删除
#group by 与count(*) 分组之后在统计个数
SELECT manager_id,COUNT(*)
FROM employees
GROUP BY manager_id
#三表连接 等值连接
SELECT employee_id,department_name,city
FROM departments d,employees e,locations l
WHERE d.`department_id`=e.`department_id`
AND d.`location_id`=l.`location_id`
#非等值连接 求员工工资所对应的等级
SELECT salary,grade
FROM employees ,job_grade
WHERE salary BETWEEN min_salary AND max_salary
ORDER BY salary ASC
#自连接 在同一个表中找多遍(将表复制一遍) 查询员工和上级名称
`employees`
#---------------------------练习
#显示员工表的最大工资平均工资
SELECT MAX(salary),AVG(salary)
FROM employees
#查询员工表job_id中包含a和e的,并且a在e前面
SELECT job_id
FROM employees
WHERE job_id LIKE'%a%e%'
#查询员工表的employee_id,job_id,last_name 按照department_id降序,salary升序 有问题
SELECT employee_id,job_id,last_name,salary
FROM employees
ORDER BY department_id DESC,salary ASC
#显示当前日期 以及去前后空格 截取子字符串的函数
SELECT NOW();
SELECT TRIM(' sadasd ');
SELECT SUBSTR('你好世界',3,2);
#查询90号部门的job_id 和90号部门的location_id
SELECT d.department_id,job_id,location_id
FROM departments d,employees e
WHERE d.`department_id`=90
AND d.`department_id`=e.`department_id`
#查询每个国家下部门大于二的国家id
SELECT COUNT(*),`locations`.`country_id`
FROM `departments` d,`locations` l
WHERE d.`location_id`=`locations`.`location_id`
GROUP BY country_id
HAVING COUNT(*)>=2
#选择city在toronto工作的员工的last_name,job_id,department_id,department_name
SELECT last_name,job_id,d.department_id,department_name,city
FROM employees e,locations l,departments d
WHERE l.city='toronto'
AND d.`department_id`=e.`department_id`
AND d.`location_id`=l.`location_id`
#-----------`employees`
SELECT RAND ;
#日期函数
SELECT DATEDIFF(NOW(),'2000-08-17')
SELECT MONTHNAME(NOW())
#---1999连接 内连接inner(相同部分) 外连接(左外 left[outer] 右外right[outer] 全外full[outer] ) 交叉连接cross
#查询名字中包含e的员工名,和工种名
SELECT last_name,job_title
FROM employees e
INNER JOIN jobs j
ON j.job_id=e.job_id
WHERE last_name LIKE '%e%'
#查询部门员工个数大于3的部门名和员工个数,并按个数降序
SELECT department_name,COUNT(*) ,e.department_id
FROM employees e
INNER JOIN `departments` d
ON e.department_id=d.department_id
GROUP BY e.department_id
HAVING COUNT(*)>3
ORDER BY COUNT(*) DESC
#非等值连接
#查询员工的工资级别大于2的个数,
SELECT salary,grade,COUNT(*)
FROM employees e
INNER JOIN job_grade j
ON e.Salary BETWEEN j.min_salary AND j.max_salary
GROUP BY grade
ORDER BY grade ASC
#自连接
#查询员工的名字,上级的名字
SELECT e1.employee_id,e1.last_name 员工名,e2.last_name 上级名
FROM employees e1
INNER JOIN employees e2
ON e1.manager_id=e2.employee_id
ORDER BY e1.employee_id
#外连接(显示相同部分,不同部分也显示,但为空 =内连接+主表中有而从表中没有的记录)
#查询哪个部门没有员工
SELECT d.*,e.employee_id
FROM departments d
LEFT OUTER JOIN employees e
ON d.department_id=e.department_id
WHERE e.department_id IS NULL
ORDER BY d.department_id ASC
#全外连接(内连接+表一中有而表二没有的+表二中有的表一没有) mysql不支持
#交叉连接 及笛卡尔乘积
#-------------题
#查询哪个城市没有部门
SELECT city,department_id
FROM locations l
LEFT JOIN departments d
ON l.location_id=d.location_id
WHERE d.department_id IS NULL
#查询部门名为SAL和IT的员工信息
SELECT department_name,e.*
FROM departments d
LEFT JOIN employees e
ON d.department_id=e.department_id
WHERE d.department_name IN('SAL','IT')
#______________
#子查询 子查询的执行优先于主查询的执行
#标量子查询 > < 等 ,列子查询 in some/any 某一个成立即可 all 等 ,行子查询 =
#---------------------------------------------------------------------------
#where后的标量子查询
#返回job_id与141员工相同,工资比143号多的员工的 姓名 job_id salary
SELECT last_name ,job_id,salary
FROM employees
WHERE job_id=(
SELECT job_id
FROM employees
WHERE employee_id=141
)AND salary>(
SELECT salary
FROM employees
WHERE employee_id=143
)
#列子查询
#返回其他部门中比IT部门中比所有工资都低的员工的员工号 姓名 jod_id Salary
)
#行子查询 不常用 了解
#查询员工编号最小,且工资最高的信息
SELECT *
FROM employees
WHERE (employee_id,salary)=(
SELECT MIN(employee_id),MAX(salary)
FROM employees
)
#------------------------------------
#select子查询 只支持一行一列
#查询每个部门的员工个数
SELECT d.* , (
SELECT COUNT(*)
FROM employees e
WHERE e.department_id=d.department_id
)员工数
FROM departments d
#from子查询,将子查询结果充当一张表,要求必须起别名
#exists 子查询 判断是否存在,大多数可用where子查询in代替
#exists 子查询 查询有员工的部门名
SELECT department_name,COUNT(*)
FROM departments d
WHERE EXISTS(
SELECT *
FROM employees e
WHERE d.department_id=e.department_id
)
#------以上内容为---------连接查询 ---------------子查询
#子查询题
#查询和Zlotkey相同部门的员工姓名和编号
SELECT last_name 员工,employee_id 编号
FROM employees e
WHERE e.department_id=(
SELECT f.department_id
FROM employees f
WHERE f.last_name='Zlotkey'
)
#--分页查询 放在查询语句最后 limit 索引(从零开始) 个数
SELECT *
FROM employees
LIMIT 0,2;
#联合查询 union 自动去重 union all 不去重
/*
查询语句一
union
查询语句二
union
...
*/
#应用场景 查询的结果来自于多个表,之间没有直接关联信息,且查询的信息对应一致
#插入修改删除
#插入 insert into 表名 values 数值
INSERT INTO jobs(`job_id`,`job_title`,`min_salary`,`max_salary`)
VALUES('ffq','st',5000,90000),('f_nqg','stm',5000,90000)
#修改 update 表名 set值 where...
#修改location-id 等于1000的部门名为QQ
UPDATE departments d
INNER JOIN locations l
ON d.location_id=l.location_id
SET d.department_name='QQ'
WHERE l.location_id=1000
#删除delete /truncate
# delete 表名 from 表 where 筛选条件
#truncate 表名
DELETE test FROM test WHERE 是啥='ss'
TRUNCATE test
#-----------------------------------------------------------------------
#DDL语言
#-------初级
USE firstbase
#库的创建
CREATE DATABASE firstbase;
#表的创建
CREATE TABLE myfrist(
employ_id INT,
employ_ad CHAR(20),
employ_de CHAR(20)
)
#表的修改 add change modify drop (rename to)
#@增加列
ALTER TABLE myfrist
ADD num INT NULL
#@修改表名
ALTER TABLE myfrist
CHANGE COLUMN job e_time INT
#@修改列的类型或约束
ALTER TABLE myfrist
MODIFY COLUMN e_time DATETIME
#@删除列
ALTER TABLE myfrist
DROP COLUMN e_time
#@修改表名
ALTER TABLE myfrist
RENAME TO employees
#表的删除
DROP TABLE employees;
#
SHOW ENGINES
SELECT @@tx_isolation
表的复制
数据类型
表的修改和删除
六大约束
标识列(auto_increment自动增长)
事务(执行中不可分割)
SET autocommit=0;
#编写
UPDATE employees SET salary=salary+1000 WHERE employee_id=100;
SAVEPOINT a;#设置保存点
UPDATE employees SET salary=salary+1000 WHERE employee_id=101;
#结束
COMMIT;
#回滚
ROLLBACK TO a;
delete 删除的信息可以回滚
truncate 删除的信息不可以回滚
视图的创建修改
创建
create view 视图名
AS
查询语句
视图的修改
变量
自定义变量
- -用户变量
- -局部变量
存储过程
例
DELIMITER @
CREATE PROCEDURE put(IN s_alary INT)
BEGIN
SELECT *
FROM employees
WHERE salary>s_alary;
END @
CALL put(10100);
DELIMITER @
CREATE PROCEDURE put1(IN s_alary1 INT,IN s_alary2 INT)
BEGIN
SELECT *
FROM employees
WHERE salary BETWEEN s_alary1 AND s_alary2;
END @
CALL put1(10100,20000);
函数
DROP FUNCTION cal;
DELIMITER $
CREATE FUNCTION cal(s_alary INT) RETURNS INT#注意returns
BEGIN
#declare sum default 0;#局部变量
SET @sum = 10;#用户变量用@
SELECT COUNT(*) INTO @sum
FROM employees
WHERE salary>s_alary;
RETURN @sum;
END$
SELECT cal(10000)$;
流程控制结构