数据库mysql基础
创建库:create database 库名
使用库:use 库名;
创建表:create table 表名
删除表:drop table 表名
删除库: drop database 库名
两个表链接:union
给表插入数据:insert into 表名(列名1,2,3,4)values(值1,2,3,4)
(如果不指定会按顺序自动填入表中)
更新数据:update 表明 set 列名=值 where 条件
删除数据:delete from 表名 where 条件
查询语句:
查询某个表的所有信息: select * from 表名
查询带条件的信息: select * from 表名 where
排序查询: select * from 表名 order by 列名 desc 降序 //asc 升序(默认是升序)
在查询中合并重复的结果: select distinct(列名) from 表名
查询中限制记录数 :select * from 表名 limit 2,4 (取2-4条记录)
二、select * from 表名 limit 8 (直接取8条记录)
范围查询: select * from 表名 where 列名 betwee 60 and 90
(列名属于60-90的范围 下限是60,上限是90)
分组查询: select * from 表名 group by group by 列名
带条件的分组查询:select * from 表名 group by group by 列名 having 条件
子查询 select * from 表名 where 列名=(select * from 表名 where 列名)
多表联查 from 涉及的表名 where后面需要给出所有主外关联的键
一、select sname ,cname from student ,score where student.id=score.id
二、内连接: inner join on
select * from 表名 inner join 表名 on 表名.列名=表名.列名;
select * from 表名 inner join 表名 on 表名.列名=表名.列名 inner join表名.列名=表名.列名;
左外连接: left join on
右外连接 right join on
select * from 表名 inner join 表名 on 表名.列名=表名.列名;将inner 改成left/right即可
模糊查询:%_这两个用的机会比较多。
%查询任意
_只查询一个字符
[]范围查询
逻辑 查询: and or in 。in(在多个查询中使用)
and 和or 多在 where 和having 中带条件中使用
聚合函数(列名):
conunt 计算个数
sum 计算总和
avg 求平均值
max 求最大值
min求最小值
这个聚合函数不能和where一起使用
select sum(列名)from 表名;
閱讀更多 想學c的阿呆 的文章