嗨, 大家好: 网站在新年后正式开通,欢迎大家加入我们,共同维护一个良好氛围的中国技术社区。 欢迎举报不良帖子,欢迎提供建议。
我们的QQ群:136102234,欢迎进群找答案,里边会有很多资深的大师们。 point overflow

整个sql大致如下:

select 字段
from 表
join 其他表 on 关联条件
where 条件
group by 分组字段
having 分组后的筛选
limit 限制条数

之前已有介绍select,from ,limit和join.

简介:

where:在join(如果有)之后,针对合成的虚拟表做条件筛选
having:二次筛选,和where类似,不过可以放到group by之后

asked 2016-03-17 09:59:46

jkant's gravatar image

jkant ♦♦
11135138139
accept rate: 3%


我们已mysql样例库employees的表employees来做说明:

CREATE TABLE employees (
emp_no int(11) NOT NULL,
birth_date date NOT NULL,
first_name varchar(14) NOT NULL,
last_name varchar(16) NOT NULL,
gender enum('M','F') NOT NULL,
hire_date date NOT NULL,
PRIMARY KEY (emp_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

select month(birth_date) as '月份',count(*) as '数量'
from employees
-- where 条件
where birth_date>'1929-102-03'
-- and or 条件 or需要括号
and( gender='M' or first_name='Guoxiang')

-- 分组
group by month(birth_date)

-- 二次筛选,即之前的数据作为虚拟表,再做条件筛选

having 数量>0

limit 10;

link

answered 2016-03-17 10:08:26

jkant's gravatar image

jkant ♦♦
11135138139
accept rate: 3%

已编辑 2016-03-17 10:11:01

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×72
×7
×2
×1

Asked: 2016-03-17 09:59:46

Seen: 1,342 times

Last updated: 2016-03-17 10:11:01