查看: 93|回复: 1

MySQL基础用法(1)

[复制链接]

3

主题

6

帖子

11

积分

新手上路

Rank: 1

积分
11
发表于 2023-1-5 11:12:16 | 显示全部楼层 |阅读模式
1.安装和测试

参考:windows10上安装mysql(详细步骤)_他们叫我周周周的博客-CSDN博客_mysql安装
2.操作数据库


  • 连接数据库:mysql -u 用户名 -p + 回车键 再输入密码
  • 退出数据库:exit或者quit
  • 查看已经有的数据库:show databases;(记得有s)
  • 查看数据库版本:select version(); (SQL语句分号不能少)
  • 创建数据库:

    • create database 数据库名称;(如:create databases test; 创建一个叫做test的数据库)
    • create database 数据库名称 charset = utf8;(指定编码为utf8)

  • 查看数据库创建的命令:show create database 数据库名称;(比如:show create database test;)



  • 查看当前使用的数据库:select database(); 括号不能忘记。
  • 使用数据库:use 数据库名称;(比如:use test;)
  • 删除数据库:drop database 数据库名称;(比如:drop database test2;)
3.操作数据表


  • 查看当前库中所有表:show tables;
  • 创建表:create table 表明(字段名 类型 约束);比如(create table test(age int);)
  • 查看表的创建语句:show create table 数据表名;(show create table test;)
  • 查看表描述信息:describe 数据表名; (describe test;)
  • 添加表字段:alter table 数据表名称 add 字段名称 字段类型;(alter table test add salary double;)
  • 修改表字段类型

    • 不重新命名 alter table 数据表名称 modify 字段 新的字段类型;(alter table test modify salary int;)
    • 重新命名 alter table 数据表名称 change 旧字段名 新字段名 新数据类型;(alter table test change height h int;)将原来的字段height修改为h同时数据类型为int。

  • 删除表字段:alter table 数据表名称 drop 字段;(alter table test drop h;)
4.操作数据

1.新增修改删除数据


  • 整行插入:insert into 表名 values(?, ?, ?, ...)所有列都要加
  • 指定列中插入数据:insert into 表名(指定的列) values(指定列对应的数据)

    • insert into test(age, salary) values(24, 22059.02);

  • 指定列中插入多条数据:insert into 表名(指定列) values(对应的值1), (对应的值2),(对应的值3);

    • insert into test(age, salary) values(28, 30129.29), (32, 42139.08);

  • 修改数据:update 数据表名 set 字段 = 新值 where 字段 = 某个值

    • update set test salary = 22896.07 where age = 24;

  • 删除数据:delete from 数据表名称 where 字段 = 某个值;

    • delete from test where age = 18;

2.查询数据


  • 查询整个表数据:select * from 表名;(select * from test;)
  • 查询指定字段数据:select 字段1,字段2,字段n from test;(select age from test;)
  • 查询指定字段数据,并给字段起别名:select 字段1 as 别名1, 字段2 as 别名2 from 数据表名

    • select age as a, salary as s from test; select age as a from test;

  • 查询指定字段并去重:select distinct 字段1,字段2,字段n from 数据表名称;

    • 如果是多个字段一起去重那要全部一样才行,不然就不会去重。
    • select distince age from test; select distinct age, salary from test;

  • where子句:用于做筛选条件

    • 比如age=18的时候的数据select * from test where age = 18;
    • where子句还可以结合运算符一起使用

      • 比较运算符:=、>、>=、<、<=、<>(这个是不等于)、逻辑运算符:and、or、not a(不是a)

        • select * from test where age >= 20;
        • select * from test where age <> 20;

      • 模糊查询:like + % / _:%表示任意多个字符,_表示任意一个字符。

        • select * from test where age like "%"; %表示多个字符
        • select * from test where age like "_5"; _表示一个字符 后面那个字符必须是5

      • 范围查询:两个关键字 一个是in(非连续),一个是between...and...(连续范围)

        • in:表示在一个离散取值的范围内。

          • select * from test where age in (18, 20, 22);表示选取test表中年龄等于18 20 22这三的数据
          • select * from test where age not in (18, 20, 22);

        • between...and:表示在一个连续范围内。

          • select * from test where age between 20 and 27;选在20-27范围内的数据
          • select * from test where age (between 20 and 27) and sex = 1;




5.聚合函数


  • count:总数

    • 查询表中一共有多少条数据:select count(*) from test;




  • 查询表中某一种数据的总数:比如查年龄大于等于25的总人数

    • select count(*) from test where age >= 25;




  • max():最大值

    • 比如查询最大的年龄:select max(age) from test;

  • min():最小值

    • 比如查询薪资的最小值:select min(salary) from test;

  • sum():求和

    • 查询所有薪资的和:select sum(salary) from test;

  • avg():平均值

    • 查询平均薪资:select avg(salary) from test;

6.分组筛选

通过group by 将查询结果按照1个或者多个字段进行分组,字段相同的为一组。比如按照班级进行group by,一个班级的在一个组。
select *(字段名) from 数据表名称 group by 需要分组的字段名;
分组后的筛选:原始数据集根据where以后得到结果集1;结果集1再根据group by得到结果集2;结果集2再根据having得到结果集3。having是group by以后用来筛选的。
待补充...
7.排序


  • order by 字段 默认升序
  • order by asc 字段 指定升序
  • order by desc 字段 指定降序
待补充...
8.限制


  • 语法:limit start, count

    • start:偏移量,默认为0
    • count:要选出来的条数

  • 注意点:limit只能写在最后面
9.表连接

内连接+外连接。外连接 = 左 + 右连接
1.内连接


  • 仅选出两张表中互相匹配的记录,公共的数据。A表B表都有的。
  • select * from 表1 inner join 表2 on 表1.列 = 表2.列;
  • select * from train inner join test on train.age = test.age;
2.左连接


  • 左表有的数据,如果右表没有的话就用null填充。
  • select * from 表1 left join 表2 on 表1.列 = 表2.列;
  • select * from train left join test on train.age = test.age;
3.右连接


  • 右表有的数据,如果左表中不存在就用null填充。
  • select * from 表1 right join 表2 on 表1.列 = 表2.列;
10.子查询

待补充...
参考链接:
Sql语句 · 语雀
回复

使用道具 举报

2

主题

13

帖子

21

积分

新手上路

Rank: 1

积分
21
发表于 2025-4-11 05:53:00 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表