码迷,mamicode.com
首页 > 数据库 > 详细

mysql语句基本简单查询

时间:2017-02-24 15:17:36      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:ber   查看   date   esc   var   注释   规则   2.7   条件查询   

mysql语句基本简单查询

 

第一节

 

-- or # 单行注释
/**
*多行注释
*/

-- c创建数据库exam
CREATE DATABASE exam;

USE exam;

/*创建部门表*/
CREATE TABLE dept(
deptno INT PRIMARY KEY,
dname VARCHAR(50),
loc VARCHAR(50)
);

/*创建雇员表*/
CREATE TABLE emp(
empno INT PRIMARY KEY,
ename VARCHAR(50),
job VARCHAR(50),
mgr INT,
hiredate DATE,
sal DECIMAL(7,2),
COMM DECIMAL(7,2),
deptno INT,
CONSTRAINT fk_emp FOREIGN KEY(mgr) REFERENCES emp(empno)
);

/*创建工资等级表*/
CREATE TABLE salgrade(
grade INT PRIMARY KEY,
losal INT,
hisal INT
);

/*创建学生表*/
CREATE TABLE stu(
sid INT PRIMARY KEY,
sname VARCHAR(50),
age INT,
gander VARCHAR(10),
province VARCHAR(50),
tuition INT
);

 

 

 

/*插入dept表数据*/
INSERT INTO dept VALUES (10, ‘教研部‘, ‘北京‘);
INSERT INTO dept VALUES (20, ‘学工部‘, ‘上海‘);
INSERT INTO dept VALUES (30, ‘销售部‘, ‘广州‘);
INSERT INTO dept VALUES (40, ‘财务部‘, ‘武汉‘);

/*插入emp表数据*/
INSERT INTO emp VALUES (1009, ‘曾阿牛‘, ‘董事长‘, NULL, ‘2001-11-17‘, 50000, NULL, 10);
INSERT INTO emp VALUES (1004, ‘刘备‘, ‘经理‘, 1009, ‘2001-04-02‘, 29750, NULL, 20);
INSERT INTO emp VALUES (1006, ‘关羽‘, ‘经理‘, 1009, ‘2001-05-01‘, 28500, NULL, 30);
INSERT INTO emp VALUES (1007, ‘张飞‘, ‘经理‘, 1009, ‘2001-09-01‘, 24500, NULL, 10);
INSERT INTO emp VALUES (1008, ‘诸葛亮‘, ‘分析师‘, 1004, ‘2007-04-19‘, 30000, NULL, 20);
INSERT INTO emp VALUES (1013, ‘庞统‘, ‘分析师‘, 1004, ‘2001-12-03‘, 30000, NULL, 20);
INSERT INTO emp VALUES (1002, ‘黛绮丝‘, ‘销售员‘, 1006, ‘2001-02-20‘, 16000, 3000, 30);
INSERT INTO emp VALUES (1003, ‘殷天正‘, ‘销售员‘, 1006, ‘2001-02-22‘, 12500, 5000, 30);
INSERT INTO emp VALUES (1005, ‘谢逊‘, ‘销售员‘, 1006, ‘2001-09-28‘, 12500, 14000, 30);
INSERT INTO emp VALUES (1010, ‘韦一笑‘, ‘销售员‘, 1006, ‘2001-09-08‘, 15000, 0, 30);
INSERT INTO emp VALUES (1012, ‘程普‘, ‘文员‘, 1006, ‘2001-12-03‘, 9500, NULL, 30);
INSERT INTO emp VALUES (1014, ‘黄盖‘, ‘文员‘, 1007, ‘2002-01-23‘, 13000, NULL, 10);
INSERT INTO emp VALUES (1011, ‘周泰‘, ‘文员‘, 1008, ‘2007-05-23‘, 11000, NULL, 20);

INSERT INTO emp VALUES (1001, ‘甘宁‘, ‘文员‘, 1013, ‘2000-12-17‘, 8000, NULL, 20);


/*插入salgrade表数据*/
INSERT INTO salgrade VALUES (1, 7000, 12000);
INSERT INTO salgrade VALUES (2, 12010, 14000);
INSERT INTO salgrade VALUES (3, 14010, 20000);
INSERT INTO salgrade VALUES (4, 20010, 30000);
INSERT INTO salgrade VALUES (5, 30010, 99990);

/*插入stu表数据*/
INSERT INTO `stu` VALUES (‘1‘, ‘王永‘, ‘23‘, ‘男‘, ‘北京‘, ‘1500‘);
INSERT INTO `stu` VALUES (‘2‘, ‘张雷‘, ‘25‘, ‘男‘, ‘辽宁‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘3‘, ‘李强‘, ‘22‘, ‘男‘, ‘北京‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘4‘, ‘宋永合‘, ‘25‘, ‘男‘, ‘北京‘, ‘1500‘);
INSERT INTO `stu` VALUES (‘5‘, ‘叙美丽‘, ‘23‘, ‘女‘, ‘北京‘, ‘1000‘);
INSERT INTO `stu` VALUES (‘6‘, ‘陈宁‘, ‘22‘, ‘女‘, ‘山东‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘7‘, ‘王丽‘, ‘21‘, ‘女‘, ‘北京‘, ‘1600‘);
INSERT INTO `stu` VALUES (‘8‘, ‘李永‘, ‘23‘, ‘男‘, ‘北京‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘9‘, ‘张玲‘, ‘23‘, ‘女‘, ‘广州‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘10‘, ‘啊历‘, ‘18‘, ‘男‘, ‘山西‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘11‘, ‘王刚‘, ‘23‘, ‘男‘, ‘湖北‘, ‘4500‘);
INSERT INTO `stu` VALUES (‘12‘, ‘陈永‘, ‘24‘, ‘男‘, ‘北京‘, ‘1500‘);
INSERT INTO `stu` VALUES (‘13‘, ‘李雷‘, ‘24‘, ‘男‘, ‘辽宁‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘14‘, ‘李沿‘, ‘22‘, ‘男‘, ‘北京‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘15‘, ‘王小明‘, ‘25‘, ‘男‘, ‘北京‘, ‘1500‘);
INSERT INTO `stu` VALUES (‘16‘, ‘王小丽‘, ‘23‘, ‘女‘, ‘北京‘, ‘1000‘);
INSERT INTO `stu` VALUES (‘17‘, ‘唐宁‘, ‘22‘, ‘女‘, ‘山东‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘18‘, ‘唐丽‘, ‘21‘, ‘女‘, ‘北京‘, ‘1600‘);
INSERT INTO `stu` VALUES (‘19‘, ‘啊永‘, ‘23‘, ‘男‘, ‘北京‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘20‘, ‘唐玲‘, ‘23‘, ‘女‘, ‘广州‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘21‘, ‘叙刚‘, ‘18‘, ‘男‘, ‘山西‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘22‘, ‘王累‘, ‘23‘, ‘男‘, ‘湖北‘, ‘4500‘);
INSERT INTO `stu` VALUES (‘23‘, ‘赵安‘, ‘23‘, ‘男‘, ‘北京‘, ‘1500‘);
INSERT INTO `stu` VALUES (‘24‘, ‘关雷‘, ‘25‘, ‘男‘, ‘辽宁‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘25‘, ‘李字‘, ‘22‘, ‘男‘, ‘北京‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘26‘, ‘叙安国‘, ‘25‘, ‘男‘, ‘北京‘, ‘1500‘);
INSERT INTO `stu` VALUES (‘27‘, ‘陈浩难‘, ‘23‘, ‘女‘, ‘北京‘, ‘1000‘);
INSERT INTO `stu` VALUES (‘28‘, ‘陈明‘, ‘22‘, ‘女‘, ‘山东‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘29‘, ‘孙丽‘, ‘21‘, ‘女‘, ‘北京‘, ‘1600‘);
INSERT INTO `stu` VALUES (‘30‘, ‘李治国‘, ‘23‘, ‘男‘, ‘北京‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘31‘, ‘张娜‘, ‘23‘, ‘女‘, ‘广州‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘32‘, ‘安强‘, ‘18‘, ‘男‘, ‘山西‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘33‘, ‘王欢‘, ‘23‘, ‘男‘, ‘湖北‘, ‘4500‘);
INSERT INTO `stu` VALUES (‘34‘, ‘周天乐‘, ‘23‘, ‘男‘, ‘北京‘, ‘1500‘);
INSERT INTO `stu` VALUES (‘35‘, ‘关雷‘, ‘25‘, ‘男‘, ‘辽宁‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘36‘, ‘吴强‘, ‘22‘, ‘男‘, ‘北京‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘37‘, ‘吴合国‘, ‘25‘, ‘男‘, ‘北京‘, ‘1500‘);
INSERT INTO `stu` VALUES (‘38‘, ‘正小和‘, ‘23‘, ‘女‘, ‘北京‘, ‘1000‘);
INSERT INTO `stu` VALUES (‘39‘, ‘吴丽‘, ‘22‘, ‘女‘, ‘山东‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘40‘, ‘冯含‘, ‘21‘, ‘女‘, ‘北京‘, ‘1600‘);
INSERT INTO `stu` VALUES (‘41‘, ‘陈冬‘, ‘23‘, ‘男‘, ‘北京‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘42‘, ‘关玲‘, ‘23‘, ‘女‘, ‘广州‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘43‘, ‘包利‘, ‘18‘, ‘男‘, ‘山西‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘44‘, ‘威刚‘, ‘23‘, ‘男‘, ‘湖北‘, ‘4500‘);
INSERT INTO `stu` VALUES (‘45‘, ‘李永‘, ‘23‘, ‘男‘, ‘北京‘, ‘1500‘);
INSERT INTO `stu` VALUES (‘46‘, ‘张关雷‘, ‘25‘, ‘男‘, ‘辽宁‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘47‘, ‘送小强‘, ‘22‘, ‘男‘, ‘北京‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘48‘, ‘关动林‘, ‘25‘, ‘男‘, ‘北京‘, ‘1500‘);
INSERT INTO `stu` VALUES (‘49‘, ‘苏小哑‘, ‘23‘, ‘女‘, ‘北京‘, ‘1000‘);
INSERT INTO `stu` VALUES (‘50‘, ‘赵宁‘, ‘22‘, ‘女‘, ‘山东‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘51‘, ‘陈丽‘, ‘21‘, ‘女‘, ‘北京‘, ‘1600‘);
INSERT INTO `stu` VALUES (‘52‘, ‘钱小刚‘, ‘23‘, ‘男‘, ‘北京‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘53‘, ‘艾林‘, ‘23‘, ‘女‘, ‘广州‘, ‘2500‘);
INSERT INTO `stu` VALUES (‘54‘, ‘郭林‘, ‘18‘, ‘男‘, ‘山西‘, ‘3500‘);
INSERT INTO `stu` VALUES (‘55‘, ‘周制强‘, ‘23‘, ‘男‘, ‘湖北‘, ‘4500‘);



-- 1 基础查询
-- 1.1 查询所有列

SELECT * FROM stu;

-- 1.2 在stu表中查询指定列 sid, sname, age

SELECT sid, sname, age FROM stu;

 

 


-- 2 条件查询
-- 2.2 查询性别为女,并且年龄50的记录
 SELECT * FROM stu WHERE gender=‘female‘ AND ge<50;

-- 2.3 查询学号为1,或者姓名为王的记录
SELECT * FROM stu WHERE sid =‘1‘ OR sname=‘王‘;

-- 2.4 查询学号为1,2,3的记录
SELECT * FROM stu WHERE sid IN (‘1‘,‘2‘,‘3‘);

-- 2.5 查询学号不是1,2,3的记录
SELECT * FROM tab_student WHERE s_number NOT IN (‘1‘,‘2‘,‘3‘);

-- 2.6 查询年龄为22的记录
SELECT * FROM stu WHERE age IS 22;

-- 2.7 查询年龄在20到24之间的学生记录
SELECT * FROM stu WHERE age>=20 AND age<=24;
-- 或者
SELECT * FROM stu WHERE age BETWEEN 20 AND 24;
-- 或者
SELECT * FROM stu WHERE age(20,21,22,23,24);

-- 2.8 查询性别非男的学生记录
SELECT * FROM stuWHERE gender!=‘男‘;
-- 或者
SELECT * FROM stuWHERE gender<>‘男‘;
-- 或者
SELECT * FROM stuWHERE NOT gender=‘男‘;
-- 或者
SELECT * FROM stuWHERE gender=‘女‘;

-- 2.9 查询姓名不为null的学生记录
SELECT * FROM stu WHERE NOT sname IS NULL;
-- 或者
SELECT * FROM stu WHERE sname IS NOT NULL;

 

 


-- 3 模糊查询
-- 3.1 查询姓名由5个字母构成的学生记录
SELECT * FROM stu WHERE sname LIKE ‘_____‘;


-- 3.2 查询姓名由5个字母构成,并且第5个字母为“i”的学生记录
SELECT * FROM stu WHERE sname LIKE ‘____i‘;

-- 3.3 查询姓名以“张”开头的学生记录
SELECT * FROM stu WHERE sname LIKE ‘张%‘;


-- 3.4 查询姓名中第2个字母为“小”的学生记录
SELECT * FROM stu WHERE sname LIKE ‘_小%‘;

-- 3.5 查询姓名中包含“丽”字母的学生记录
SELECT * FROM stu WHERE sname LIKE ‘%丽%‘;

 


-- 4 字段控制查询
-- 4.1 去除重复记录
SELECT DISTINCT sal FROM emp;

-- 4.2 查看雇员的月薪与佣金之和  
SELECT *,sal+comm FROM emp;

-- comm列有很多记录的值为NULL,因为任何东西与NULL相加结果还是NULL,所以结算结果可能会出现NULL。下面使用了把NULL转换成数值0的函数IFNULL:
SELECT *,sal+IFNULL(comm,0) FROM emp;

--- 4.3 给列名添加别名
-- 在上面查询中出现列名为sal+IFNULL(comm,0),这很不美观,现在我们给这一列给出一个别名,为total:

SELECT *, sal+IFNULL(comm,0) AS total FROM emp;

-- 给列起别名时,是可以省略AS关键字的:
SELECT *,sal+IFNULL(comm,0) total FROM emp;

 

 

-- 5 排序 ORDER BY
-- 5.1 查询所有学生记录,按年龄升序排序
SELECT * FROM stu ORDER BY sage ASC;
-- 或者
SELECT * FROM stu ORDER BY sage;

-- 5.2 查询所有学生记录,按年龄降序排序
SELECT * FROM stu ORDER BY age DESC;

-- 5.3 查询所有雇员,按月薪降序排序,如果月薪相同时,按编号升序排序
SELECT * FROM empORDER BY sal DESC,empno ASC;

 


-- 6.1 COUNT
-- 查询emp表中记录数:
SELECT COUNT(*) AS cnt FROM emp;

-- 查询emp表中有佣金的人数:
SELECT COUNT(comm) cnt FROM emp;

-- 查询emp表中月薪大于2500的人数:
SELECT COUNT(*) FROM empWHERE sal > 2500;

-- 统计月薪与佣金之和大于2500元的人数:
SELECT COUNT(*) AS cnt FROM emp WHERE sal+IFNULL(comm,0) > 2500;


-- 查询有佣金的人数,以及有领导的人数:
SELECT COUNT(comm), COUNT(mgr) FROM emp;

-- 6.2 SUM和AVG
-- 查询所有雇员月薪和:
SELECT SUM(sal) FROM emp;

-- 查询所有雇员月薪和,以及所有雇员佣金和:
SELECT SUM(sal), SUM(comm) FROM emp;

-- 查询所有雇员月薪+佣金和:
SELECT SUM(sal+IFNULL(comm,0)) FROM emp;

-- 统计所有员工平均工资:
SELECT SUM(sal), COUNT(sal) FROM emp;
-- 或者
SELECT AVG(sal) FROM emp;

-- 6.3 MAX和MIN
-- 查询最高工资和最低工资:
SELECT MAX(sal), MIN(sal) FROM emp;

 

 

-- 7.1 分组查询
-- 查询每个部门的部门编号和每个部门的工资和:
SELECT deptno, SUM(sal) FROM emp GROUP BY deptno;

-- 查询每个部门的部门编号以及每个部门的人数:
SELECT deptno,COUNT(*) FROM emp GROUP BY deptno;

-- 查询每个部门的部门编号以及每个部门工资大于1500的人数:
SELECT deptno,COUNT(*) FROM emp WHERE sal>1500 GROUP BY deptno;

-- 7.2 HAVING子句
-- 查询工资总和大于9000的部门编号以及工资和:
SELECT deptno, SUM(sal) FROM emp GROUP BY deptno HAVING SUM(sal) > 9000;

 


-- 8.1 查询5行记录,起始行从0开始
SELECT * FROM emp LIMIT 0, 5;

-- 8.2 查询10行记录,起始行从3开始
SELECT * FROM emp LIMIT 3, 10;

 

8.3 分页查询 规则
(页数-1)*行数=第二页的第一个显示行数。   从几行开始就写几。(前面是起始行数,后面是显示行数)
8.3 分页查询 
查询语句书写顺序:select – from- where- group by- having- order by-limit 
查询语句执行顺序:from - where -group by - having - select - order by-limit

 

mysql语句基本简单查询

标签:ber   查看   date   esc   var   注释   规则   2.7   条件查询   

原文地址:http://www.cnblogs.com/S-X-C/p/6438558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!