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.:

sql 复制代码
select
   count(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.:

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

eg.:

sql 复制代码
select id,name,phone_number
    from contacts
    where 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.:

sql 复制代码
select count(gender) as male_num,
       round(avg(gpa),1) as avg_gpa
from user_profile 
where gender = 'male'
  1. 分组查询 : group by
    eg.
sql 复制代码
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_cnt
    from user_profile
    group by gender,university
    order by gender asc,
            university asc

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

  1. 分组后条件过滤 : having
    eg.:
sql 复制代码
 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 university
    having avg_question_cnt < 5 or avg_answer_cnt < 20
  1. 多表查询- 内连接 :inner join ,注意应指定列。
    eg.:
sql 复制代码
select q.device_id,q.question_id,q.result 
    from question_practice_detail as q
        inner join user_profile as up
    on q.device_id = up.device_id and up.university = '浙江大学'
相关推荐
yangchanghua1111 小时前
pgsql 如何查询今天范围内的数据(当天0点0分0秒 - 当天23点59分59秒....)
数据库·pgsql
larance1 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
python_chai1 小时前
从数据汇总到高级分析,SQL 查询进阶实战(下篇)—— 分组、子查询与窗口函数全攻略
数据库·sql·mysql
在努力的前端小白1 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
未来之窗软件服务1 小时前
自建知识库,向量数据库 (九)之 量化前奏分词服务——仙盟创梦IDE
数据库·仙盟创梦ide·东方仙盟·自建ai·ai分词
冒泡的肥皂5 小时前
MVCC初学demo(一
数据库·后端·mysql
.Shu.6 小时前
Redis Reactor 模型详解【基本架构、事件循环机制、结合源码详细追踪读写请求从客户端连接到命令执行的完整流程】
数据库·redis·架构
Bruce_Liuxiaowei8 小时前
MySQL完整重置密码流程(针对 macOS)
mysql
麦麦大数据8 小时前
F003疫情传染病数据可视化vue+flask+mysql
mysql·flask·vue·大屏·传染病
薛晓刚8 小时前
当MySQL的int不够用了
数据库