SQL简单记录

数据库特性:1.原子性、2.一致性、3.隔离性、4.持久性
1.use/show/create datebase; 
 
2.distinct 去重
例:SELECT DISTINCT country FROM Websites;

3.分页limit
select * from 表名 limit start,count
查询前3行男生信息
select * from students where gender=1 limit 0,3;

4.分组group by
select gender from students group by gender;

+--------+
| gender |
+--------+
| 男 |
| 女 |
| 中性 |
| 保密 |
+--------+

5.连接查询join……on 
select * from 表1 inner或left或right join 表2 on 表1.列 = 表2.列

6.排序
select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]

7.优先级

and比or先运算,如果同时出现并希望先算or,需要结合()使用

8.自关联

创建areas表的语句如下:
create table areas(
aid int primary key,
atitle varchar(20),
pid int
);
从sql文件中导入数据
source areas.sql;
查询一共有多少个省
select count(*) from areas where pid is null;

总结:完整的select语句

select distinct *
from 表名
where ....
group by ... having ...
order by ...
limit start,count

执行顺序为:
from 表名
where ....
group by ...
select distinct *
having ...
order by ...
limit start,count

原文地址:https://www.cnblogs.com/ybxw/p/12531012.html