MySql 关键字

 

名称 关键字 用法
创建 create 创建库:create database 数据库名;  创建表:create table 表名 (列名 列类型……);
删除 drop 删除库:drop database 数据库名;  删除表: drop table 表名;
选择 use use 数据库/表;
insert into

insert into 表名(列1,列2,列3)

values

(值1,值2,值3);

delete delete from 表名 where 列=值条件;
update update 表 set 列=值,列=值…… where 条件;
select select 列,列 from 表 where 条件;
去重 distinct select distinct 去重字段 from 表;
在……之间 between……and select * from user where age between 20 and 30; (查询年龄在20-30之间的用户)
模糊匹配 link select * from user where name like ‘张_%’; (其中_匹配 一个字符,%匹配 一个或多个)
取前N位 Top  
条件语句 where  
分页查询 limit SELECT * FROM user LIMIT 0,5; (查询前 5 个记录行)
排序 order by select * from user order by age;(默认从小到大的正序, asc 正序,desc倒序)
分组 group by select sex,count(*) from user group by sex;(分组查询男女总人数)
分组后筛选 having
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2

WHERE 子句在所选列上设置条件,而 HAVING 子句则在由 GROUP BY 子句创建的分组上设置条件。

在一个查询中,HAVING 子句必须放在 GROUP BY 子句之后,必须放在 ORDER BY 子句之前。

 别名  as  
SELECT column_name AS alias_name
FROM table_name;

 连接去重

 union  select 列1,列2…… from 表 where 条件 union 参数 select 列1,列2…… from 表 where 条件 (有all、distinct2个参数,all返回所有集)
 内连接  inner join

INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。

LEFT JOIN 会读取左边数据表的全部数据,即便右边表无对应数据。

 RIGHT JOIN 会读取右边数据表的全部数据,即便左边边表无对应数据。

左连接   left join
 右连接  right join

 修改修改数据表名

或者修改数据表字段

 alter

AlTER TABLE testalter_tbl DROP i;删除

ALTER TABLE testalter_tbl ADD i INT;添加

ALTER TABLE testalter_tbl MODIFY c CHAR(10);修改字段类型及名称

 记录条数  count  select COUNT(*) from user; (查询user表所有记录条数)
 求和  sum    select sum(age) from user;(查询所有的年龄和)
 最大最小值  max、min  select max(age) from user;(最大的年龄最小同理)
 平均值  avg  select avg(age) from user;(所有人年龄的平均值)

参考:https://blog.csdn.net/qq_44614710/article/details/86763114

https://www.runoob.com/mysql/mysql-functions.html

posted @ 2021-07-09 16:30  Gex  阅读(141)  评论(0编辑  收藏  举报