一、 查询:
1.去重(distinct必须放在开头)
a、作用于单列
Select distinct name from A;
b、作用于多列
select distinct name ,id from A;
c、count 统计 name去重后的数量
Select count(distinct name)from A ;
count不能统计多个字段,如果想统计多个字段,可以使用嵌套查询
Select count(*) from (select distinct age,name from B) AS M;
d、 id name => id name
1 a 1 a
2 b 2 b
3 c 3 c
4 b
5 c
Select *,count(distinct name) from table group by name
Group by 必须放在order by(升序asc 降序 desc)和limit 之前;group by 用于分类汇总,一般与聚合函数(比如avg平均、sum合计、max最 大、min最小、count计算行)一起使用。还可以有去重的作用
having:用于对where和group by查询出来的分组经行过滤,查出满足条件的分组结果。它是一个过滤声明,是在查询返回结果集以后对查询结果进行的过滤操作。语句顺序 Where>>group by>> having
所以having的使用需要注意以下几点:
a).having只能用于group by(分组统计语句中)
b).where 是用于在初始表中筛选查询,having用于在where和group by 结果分组中查询
c).having 子句中的每一个元素也必须出现在select列表中
d).having语句可以使用聚合函数,而where不使用。
2、带有条件的查询 where
Select 列名称 from 表名称 where 列 运算符 值
(运算符有=、<=、!=、>=、<、>、<>(不等于)、like、between) like’F%’
Where id between 1 and 9 (查询id 在1-9 之间,包含1,9)
3、为表添加主键
Alter table tablename add primary key (col)
4、为表添加一列
Alter table tablename add column columnname coltype
二、 插入 Insert:
语法: insert into 表名 values(值1,值2…)
(单条数据)
insert into 表名 (列1,列2…)values (‘值1’,’值2’…)
(多条数据)
insert into 表名(列1,列2…)values (值1,值2…),(值3,值4…)…
插入特定数据:
Insert into table (name,age)
Select name,age
From table
Where id=1;
三、更改数据Update:
语法: update 表名 set 列=值 where 列=值
更新单列:
Update table
Set age=17
Where id=’009’
更新多列:
Update table
Where id=90
Set name=’Long’ ,age=12;
四、删除数据Delete:
删除单条:
Delete
Where id=01
From table