单表查询
准备一张雇员表company.employee
雇员编号 emp_id int
雇员姓名 emp_name varchar(30)
雇员性别 sex enum
雇用日期 hire_date date
职位 post varchar(50)
职位描述 job_description varchar(50)
薪水 salary double(15,2)
办公室office int
部门编号 dep_id int
一、表的简单查询
Select * from employee; //查看全部
Select 字段1,字段2,...字段n from 表名
避免重复关键字distinct
Select distinct post from employee
关系运算符:+ - * 、 %
Select emp_name salary*12 from employee;
As别名
Select emp_name as姓名 salary*12 as 薪水 from employee;
定义显示格式 concat()函数用于连接字符串
Select concat (emp_name,’annual salary’ ,salary*12) as 员工年薪
From employee;
二、通过条件查询
条件查询条件表达式
比较运算符:> <= != >= <=
逻辑运算符and 或者&& or或者|| xor not或者!
语法:select 字段1,字段2,字段N from 表名 where condition(条件表达式)
单条件查询
Select emp_name from employee where post =’hr’
多条件查询
Select emp_name from employee where post=’hr’ and salayr>100000;
关键字查询between and
Select emp_name from employee
Where salary between 5000 and 15000; //工资在5000到15000之间
Select emp_name from employee
Where salary not between 5000 and 15000; // 工资不在5000到15000之间的人
关键字查询is null
Select emp_name,job_description from employee
Where job_description is null; // 查看职位描述是空的员工
Select emp_name,job_description from employee
Where job_description is not null; //查看职位描述非空的员工
本文转自 探花无情 51CTO博客,原文链接:http://blog.51cto.com/983865387/1917420