单表查询语法:
SELECT 字段1,字段2... FROM 表名
WHERE 条件
GROUP BY field
HAVING 筛选
ORDER BY field
LIMIT 限制条数
关键字的执行顺序:
这个是非常重要的,不了解以后会有很多坑。
1.form 找到表:from 2.where 拿着where指定的约束条件,去文件/表中取出一条条记录 3.group by 将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组 4.having 将分组的结果进行having过滤 5.select 执行select 6.distinct 去重 7.order by 将结果按条件排序:order by 8.limit 限制结果的显示条数
建立一个名为student的学生表,下面是他们的字段名及字段类型表。
create table student( id int not null unique auto_increment, name varchar(20) not null, sex enum('male','female') not null default 'male', age int(3) unsigned not null default 28, birthday date not null, class_id int );