当前位置: 首页 > news >正文

MySQL刷题相关简单语法集合

  1. 去重 distinct 关键字
    eg. :select distinct university from user_profile

  2. 返回行数限制: limit关键字
    eg. :select device_id from user_profile limit 2

  3. 返回列重命名:as 关键字
    eg.:select device_id as user_infos_example from user_profile limit 2

  4. 指定关键字排序:order by 关键字(ASC升序,DESC降序):
    eg:select device_id,age from user_profile order by age asc

  5. 统计总数 + 模糊条件筛选: count + where + like
    eg.:

selectcount(distinct device_id) as did_cnt,count(question_id) as question_cnt
from question_practice_detail
where date like "2021-08%"
  1. 判断非空: is not null
    eg.:select device_id,gender,age,university from user_profile where age is not null

  2. 多条件判断 : where in / not in
    eg.:

select device_id, gender, age ,university, gpafrom user_profile where university in ('北京大学','复旦大学','山东大学')
  1. 正则表达式:使用regexp 进行正则匹配
    正则表达式匹配的字符类:
    .:匹配任意单个字符。
    ^:匹配字符串的开始。
    $:匹配字符串的结束。
    *****:匹配零个或多个前面的元素。
    +:匹配一个或多个前面的元素。
    ?:匹配零个或一个前面的元素。
    [abc]:匹配字符集中的任意一个字符。
    [^abc]:匹配除了字符集中的任意一个字符以外的字符。
    [a-z]:匹配范围内的任意一个小写字母。
    [0-9]:匹配一个数字字符。
    \w:匹配一个字母数字字符(包括下划线)。
    \s:匹配一个空白字符。

eg.:

select id,name,phone_numberfrom contactswhere phone_number regexp '^[1-9][0-9]{2}-?[0-9]{4}-?[0-9]{4}$'
  1. 取最大值 : max
    eg.:select max(gpa) as gpa from user_profile where university = '复旦大学'

  2. 规定小数点位数及统计平均值: round + avg
    ROUND 函数用于把数值字段舍入为指定的小数位数。
    用法为:SELECT ROUND(column_name,decimals) FROM table_name
    eg.:

select count(gender) as male_num,round(avg(gpa),1) as avg_gpa
from user_profile 
where gender = 'male'
  1. 分组查询: group by
    eg.
select gender,university,count(device_id) as user_num, round(avg(active_days_within_30),1) as avg_active_day,round(avg(question_cnt),1) as avg_question_cntfrom user_profilegroup by gender,universityorder by gender asc,university asc

注意: 由于group by后的输出不可预估,因此在笔试时可能会出现仅使用group by 无法通过测试用例的情况,此时需按用例示范追加 order by !!!

  1. 分组后条件过滤: having
    eg.:
 select university,round(avg(question_cnt),1) as avg_question_cnt,round(avg(answer_cnt),1) as avg_answer_cnt
from user_profile group by universityhaving avg_question_cnt < 5 or avg_answer_cnt < 20
  1. 多表查询- 内连接:inner join ,注意应指定列。
    eg.:
select q.device_id,q.question_id,q.result from question_practice_detail as qinner join user_profile as upon q.device_id = up.device_id and up.university = '浙江大学'
http://www.xdnf.cn/news/501373.html

相关文章:

  • 向量数据库Qdrant的Collection参数配置说明
  • uniapp-商城-61-后台 新增商品(添加商品到数据库)
  • [STM32] 5-1 时钟树(上)
  • 解决 Three.js Raycaster 点击位置与实际交点偏差问题
  • 【双指针】供暖器
  • 【Qt】Qt常见控件的相关知识点
  • 深入浅出理解JavaScript中的setProperty方法
  • 第三章 流程控制
  • Netty的简单使用
  • 在RK3588上使用NCNN和Vulkan加速ResNet50推理全流程
  • PHP8.0版本导出excel失败
  • 高并发内存池|定长内存池的设计
  • 如何在前端页面上展示解析后的 JSON 数据?
  • deepin v23.1 音量自动静音问题解决
  • 关系代数和关系数据库语言(SQL)
  • 力扣HOT100之二叉树:108. 将有序数组转换为二叉搜索树
  • BUG调试案例一:为什么网卡芯片可以找到却PING不通?--RK3588+YT8531网络调试实录
  • 算法:分治法
  • 单调栈和单调队列
  • 使用instance着色
  • 高效完成任务:制定标准与限时完成的双重法宝
  • lc42接雨水
  • 阿里巴巴开源移动端多模态LLM工具——MNN
  • Dockerfile学习指南
  • 搜索引擎工作原理|倒排索引|query改写|CTR点击率预估|爬虫
  • Linux面试题集合(4)
  • 木材价格动态定价实战指南:多算法模型与行业案例深度解析
  • 算法题(148):排座椅
  • 实验八 基于Python的数字图像问题处理
  • MySQL 中 JOIN 和子查询的区别与使用场景