以 Employee 表为例,红色标注的为表的字段:
关键词:
IN : select id from Employee where salary
查询工资为 3000 和 8000 的员工的 id
AND: select id from Employee where salary IN(3000,8000) ANDsex="male";
查询工资为 3000 和 8000 的男性员工的 id
or:select * from Employee where age=20 or age=26; --查询年龄是 20 或者 26 的所有人的信息,多条件的查询
between ... and...:select * from Employee where id between 1 and 10; --查询 id 号为 1 到 10 之间的所有字段
GROUP BY: select salary, count(*) as Total from Employee GROUP BYsalary;
把具有相同工资的员工分组,Total 是自定义的一个字段,也可以为 hello 等等。。。
包含条件的语句可以用:having 或者是 where,其中 having 是在数据分组之后进行过滤,而 where 是在分组之前用来选择记录,并且where排除的记
录不再包括在分组中
like:select * from Employee where name
select name from Employee wherename
select name from Employee wherename
select name from Employee where name
null:select * from Employee where name is null; --查询名字为空的所有字段
select * from Employee where name not is null; --查询名字不是空的所有字段
distinct:select distinct name
order by:select * from Employee order byage; --按照年龄大小的顺序查询所有员工的信息
select * from Employee order by age, salary; --按照年龄和工资查询所有员工的信息,注意是先按照年龄排序,再按照工资排序,如果年龄有很多是一样的,则需要给他们的工资排序,如果年龄不同,则不需要给 工资排序
desc 和 asc:select * from Employee order by age desc, salary;-- 先找年龄降序排列,再按照工资升序排列,默认的排序方式是升序,所以 asc 可以省略
group by:select age from Employee group by age; --以年龄为分组,查询所有的年龄组,比如:年龄为20 的人,21的人,22 的人等等。分别把他们归为一组
select age count(*) as total from Employee group by age; -- 用 count 把分组后,每一组的数量求出来
having:select salary, group_concat(name) as names from Employee group by salary having count(name)>=50; -- 查询有多于50个人拿同样工资的人名和他们所拿的工资,比如:拿3000 元的人有10个,拿5000元的人有40个,拿8000 元的人有80个,拿10000 的人有100个,那么该查询结果将显示拿 8000 和 拿10000 元工资的名字
limit:select * from Employee limit 4; --查询前4条记录
select * from Employee limit 4,3; --从第五个记录开始,再查询后面的 3 条记录