MySQL学习笔记

文章目录

主要是mac环境下的学习笔记

一、安装

mysql社区版下载地址

1、mysql启停

python 复制代码
#启动
sudo /usr/local/mysql/support-files/mysql.server start
#停止
sudo /usr/local/mysql/support-files/mysql.server stop
#重启
sudo /usr/local/mysql/support-files/mysql.server restart

二、常用命令

1、查询所有列

sql 复制代码
select * from ${table_name}
# 查询多列,多个列名逗号分开
select ${col_name} from ${table_name}

2、去重

使用distinct关键字

sql 复制代码
select distinct ${col_name} from ${table_name}

使用group by

sql 复制代码
select ${col_name} from ${table_name} group by ${col_name}

3、限制输出条数

limit关键字

limit 子句可以被用于强制 SELECT 语句返回指定的记录数。

limit 接受一个或两个数字参数。参数必须是一个整数常量。

如果只给定一个参数,它表示返回最大的记录行数目。

如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。

为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1。

初始记录行的偏移量是 0(而不是 1)。

sql 复制代码
# 输出前两条数据
select ${col_name} from ${table_name} limit 2
#指定起始行效率更高
select device_id from user_profile limit 0,2

4、起别名关键字

as,和python中的as差不多

sql 复制代码
select device_id as user_infos_example from user_profile limit 0,2

5、根据限制条件查找信息

sql 复制代码
select device_id,university from user_profile where university="北京大学"
select device_id,gender,age,university from user_profile where age>24
#多个条件使用and连接
select device_id,gender,age from user_profile where age>=20 and age<=23

除复旦大学以外的明细。

not in ('')

sql 复制代码
select device_id,gender,age,university from user_profile where university!='复旦大学'
select device_id, gender,age,university from user_profile where university not in ("复旦大学")
#多个判断值
select device_id,gender,age,university,gpa from user_profile where university in ('北京大学','复旦大学','山东大学');

过滤空值

sql 复制代码
select device_id,gender,age,university from user_profile where age is not NULL

或条件

sql 复制代码
select device_id,gender,age,university,gpa from user_profile where university='北京大学' or gpa>3.7

多个条件用and或者or连接起来

sql 复制代码
select
  device_id,
  gender,
  age,
  university,
  gpa
from
  user_profile
where
  (gpa>3.5 and university='山东大学') 
  or (gpa>3.8 and university='复旦大学');

6、字符匹配

字符匹配

一般形式为:

列名 [NOT ] LIKE

匹配串中可包含如下四种通配符:

_:匹配任意一个字符;

%:匹配0个或多个字符;

[ ]:匹配[ ]中的任意一个字符(若要比较的字符是连续的,则可以用连字符"-"表 达 );

[^ ]:不匹配[ ]中的任意一个字符。

sql 复制代码
#查询学生表中姓'张'的学生的详细信息
SELECT * FROM 学生表 WHERE 姓名 LIKE '张%';
# 姓张名字为3个字
SELECT * FROM 学生表 WHERE 姓名 LIKE '张__';
# 查询学生表中姓'张'、姓'李'和姓'刘'的学生的情况
select * from 学生表 where 姓名 like '[张李刘]%';
# 不姓刘
SELECT 姓名 FROM 学生 WHERE 姓名 NOT LIKE '刘%';
#学号最后一位不是2、3、5
SELECT * FROM 学生表 WHERE 学号 LIKE '%[^235]'

7、排序

order by ${col_name} desc/asc

其中:desc是降序,asc是升序,如果不写,默认是升序排列;

sql 复制代码
# 学校是复旦大学且gpa最高
select gpa from user_profile where university='复旦大学' order by gpa desc limit 1
# 多列排序
select
    device_id,
    gpa,
    age
from user_profile
order by gpa asc,age asc

8、分组

sql 复制代码
# 每个学校每种性别的用户数、30天内平均活跃天数和平均发帖数量
select
  gender,
  university,
  count(device_id) as user_num,
  avg(active_days_within_30) as avg_active_days,
  avg(question_cnt) as avg_question_cnt
from user_profile
group by gender,university

9、分组过滤

聚合函数结果作为筛选条件时,不能用where,而是用having语法

sql 复制代码
select
    university,
    avg(question_cnt) as avg_question_cnt,
    avg(answer_cnt) as avg_answer_cnt
from user_profile
group by university
having avg_question_cnt<5 or avg_answer_cnt<20;

10、多表查询

基本语法

sql 复制代码
#多表连接查询语法(重点)
SELECT 字段列表
    FROM 表1  INNER|LEFT|RIGHT JOIN  表2
ON 表1.字段 = 表2.字段;

内容可以去看MySQL数据查询之多表查询

11、数据合并

union:两张毫不相干的表的查询结果拼接在一起输出,前提是两个查询的列数要相同

union all 不去重

12、划定年龄段

sql 复制代码
select
    device_id,
    gender,
    case
        when age<20 then '20岁以下'
        when age>=20 and age<=24 then '20-24岁'
        when age>=25 then '25岁及以上'
        when age is NULL then '其他'
    end age_cnt
from user_profile

13、删表

如果数据库中有这个表,就删掉,一般备份数据库中用这样的语句。

bash 复制代码
drop table if exists ${表名}

三、常用函数

1、最大值,最小值

sql 复制代码
max(col_name)
min(col_name)

2、计数

sql 复制代码
count(col_name)

3、平均数

sql 复制代码
avg(col_name)

4、小数点数控制

sql 复制代码
#小数点后一位
round(avg(col_name),1)

5、日期判定

day(date) 日

month(date) 月

year(date) 年

sql 复制代码
select 
    day(date) as day,
    count(question_id) as question_cnt
from question_practice_detail
where month(date)=8 and year(date)=2021
group by date

6、字符截取

sql 复制代码
substring_index(str,delim,count)
str:要处理的字符串
delim:分隔符
count:计数

如果count是正数,那么就是从左往右数,第N个分隔符的左边的全部内容,相反,如果是负数,那么就是从右边开始数,第N个分隔符右边的所有内容

sql 复制代码
select 
    substring_index(substring_index(profile,',',3),',',-1) as age,
    count(device_id) as number
from user_submit
group by age
相关推荐
细心的莽夫28 分钟前
集合复习(java)
java·开发语言·笔记·学习·java-ee
Caramel_biscuit29 分钟前
C++专业面试真题(1)学习
c++·学习·面试
yours_Gabriel34 分钟前
java基础:面向对象(二)
java·开发语言·笔记·学习
古月居GYH41 分钟前
Autoware内容学习与初步探索(一)
学习
JUANのWEB44 分钟前
【WEB前端】---HTML---结构---笔记
前端·笔记·html
木觞清4 小时前
Django学习第三天
python·学习·django
霖烟易辞7 小时前
MySQL中的DDL语句
数据库·mysql·ddl
阿猿收手吧!8 小时前
【MySQL】表的操作{创建/查看/修改/删除}
数据库·mysql
超维Ai编程8 小时前
mysql从入门到精通
数据库·mysql
muren9 小时前
昇思MindSpore学习笔记2-01 LLM原理和实践 --基于 MindSpore 实现 BERT 对话情绪识别
笔记·深度学习·学习