本次博客带领大家学习mysql数据库中各种函数的使用。
count 返回行的总数
案例1:
SELECT COUNT(*) FROM student;
SELECT COUNT(*) FROM student WHERE math > 90;
SELECT COUNT(*) FROM student WHERE (chinese+math+english) > 250;
count(*) 和count(列) 的区别:
sum函数返回满足where条件的行的和 - 一般使用在数值列。
案例2:
SELECT SUM(math) FROM student;
SELECT SUM(math),SUM(chinese),SUM(english) FROM student;
SELECT SUM(math+chinese+english) FROM student;
SELECT SUM(chinese)/COUNT(*) FROM student;
注意:
avg 函数返回满足where条件的一列的平均值。
案例3:
SELECT AVG(math) FROM student;
SELECT AVG(chinese+math+english) FROM student;
案例4:
SELECT MAX(chinese+math+english),MIN(chinese+math+english) FROM student;
CREATE TABLE dept(
deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
dname VARCHAR(20) NOT NULL DEFAULT "",
loc VARCHAR(13) NOT NULL DEFAULT "");
INSERT INTO dept VALUES (10,'ACCOUNTING','NEW YORK'),(20,'RESEARCH','DALLAS'),(30,'SALES','CHICAGO'),(40,'OPERATIONS','BOSTON');
CREATE TABLE emp(
empno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
ename VARCHAR(20) NOT NULL DEFAULT "",
job VARCHAR(9) NOT NULL DEFAULT "",
mgr MEDIUMINT UNSIGNED,
hiredate DATE NOT NULL,
sal DECIMAL(7,2) NOT NULL,
comm DECIMAL(7,2),
deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0);
INSERT INTO emp VALUES(7369,'SMITH','CLERK',7902,'1990-12-17',800.00,NULL,20),
(7499,'ALLEN','SALESMAN',7698,'1991-2-20',1600.00,300.00,30),
(7521,'WARD','SALESMAN',7698,'1991-2-22',1250.00,500.00,30),
(7566,'JONES','MANAGER',7839,'1991-4-2',2975.00,NULL,20),
(7654,'MARTIN','SALESMAN',7698,'1991-9-28',1250.00,1400.00,30),
(7698,'BLAKE','MANAGER',7839,'1991-5-1',2850.00,NULL,30),
(7782,'CLARK','MANAGER',7839,'1991-6-9',2450.00,NULL,10),
(7788,'SCOTT','ANALYST',7566,'1997-4-19',3000.00,NULL,20),
(7839,'KING','PRESIDENT',NULL,'1991-11-17',5000.00,NULL,10),
(7844,'TURNER','SALESMAN',7698,'1991-9-8',1500.00,NULL,30),
(7900,'JAMES','CLERK',7698,'1991-12-3',950.00,NULL,30),
(7902,'FORD','ANALYST',7566,'1991-12-3',3000.00,NULL,20),
(7900,'MILLER','CLERK',7782,'1991-1-23',1300.00,NULL,10);
CREATE TABLE salgrade(
grade MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
losal DECIMAL(17,2) NOT NULL,
hisal DECIMAL(17,2) NOT NULL
);
INSERT INTO salgrade VALUES(1,700,1200),
(2,1201,1400),
(3,1401,2000),
(4,2001,3000),
(5,3001,9999);
group by 用于对查询的结果分组统计,having子句用于限制分组显示结果。
案例5:
SELECT AVG(sal),MAX(sal),deptno FROM emp GROUP BY deptno;
SELECT AVG(sal),MIN(sal),deptno,job FROM emp GROUP BY deptno,job;
SELECT AVG(sal),deptno FROM emp GROUP BY deptno HAVING AVG(sal)<2000;
案例6:以首字母小写的方式显示所有员工emp表的姓名。
SELECT CONCAT(LCASE(SUBSTRING(ename,1,1)),SUBSTRING(ename,2)) new_name FROM emp;
SELECT CONCAT(LCASE(LEFT(ename,1)),SUBSTRING(ename,2)) new_name FROM emp;
CREATE TABLE mes(
id INT,
content VARCHAR(30),
send_time DATETIME);
-- 添加一条记录
INSERT INTO mes VALUES(1,'北京新闻',CURRENT_TIMESTAMP());
INSERT INTO mes VALUES(2,'上海新闻',NOW());
INSERT INTO mes VALUES(3,'广州新闻',NOW());
案例7:
SELECT id,content,DATE(send_time) FROM mes;
SELECT * FROM mes WHERE DATE_ADD(send_time,INTERVAL 10 MINUTE) >= NOW();
SELECT * FROM mes WHERE DATE_SUB(NOW(),INTERVAL 20 MINUTE) <= send_time ;
SELECT DATEDIFF('2011-11-11','1990-01-01') FROM DUAL;
SELECT DATEDIFF(NOW(),'2000-11-29') FROM DUAL;
SELECT DATEDIFF(DATE_ADD('2000-11-29',INTERVAL 80 YEAR),NOW()) FROM DUAL;
细节说明:
unix_timestamp() : 返回的是从1970-1-1 到现在的秒数。
from_unixtime() : 可以把一个unix_timestamp 秒数,转成指定格式的日期。
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y-%m-%d') FROM DUAL;
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y-%m-%d %H:%i:%s') FROM DUAL;
CREATE TABLE users(
id INT,
`name` VARCHAR(32) NOT NULL DEFAULT '',
pwd CHAR(32) NOT NULL DEFAULT '');
INSERT INTO users VALUES(100,'领导',MD5('ld'));
SELECT * FROM users;
案例8:
SELECT ename ,IF(comm IS NULL,0.0,comm) FROM emp;
SELECT ename ,IFNULL(comm,0.0) FROM emp;
SELECT ename,(SELECT CASE WHEN job = 'CLERK' THEN '职员'
WHEN job = 'MANAGER' THEN '经理'
WHEN job = 'SALESMAN' THEN '销售人员'
ELSE job END) AS job
FROM emp;