码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 敬请期待!


    文章目录

    • 查看表中的所有数据
    • 查看表中的指定列
    • 针对查看的列重新起名[别名]
    • 除了特定列中的数据外,在查询过程中允许进行计算
    • 查询所有未删除的数据【逻辑删除 deleted boolean default 0】
    • 查询18岁以上的人
    • 查询18岁的男生
    • 查询1989出生的用户或者所有女性用户
    • 查询不是1989年出生的用户
    • 针对null不能使用=或者!=进行判断
    • 查看口令为空的用户
    • 主要针对数值类型
      • 查询1989年到2000年出生的用户
    • 主要针对字符串类型
      • 查询所有姓zhang的用户

    查看表中的所有数据

    select * from tb_users;
    
    • 1

    用于指代表中的所有列

    查看表中的指定列

    select username,password,birth from tb_users;
    
    • 1

    针对查看的列重新起名[别名]

    select username as unmame, password pwd from tb_users;  # 这里可以使用as,也可以省略
    
    • 1

    除了特定列中的数据外,在查询过程中允许进行计算

    now()获取数据库服务器的系统当前时,年月日时分秒
    year(date) 获取指定日期的年份
    
    • 1
    • 2
    select username,year(now())-year(birth) from tb_users;
    
    • 1
    select username,year(now())-year(birth) age  from tb_users;
    
    • 1

    带条件查询 select * from 表名称 where 条件

    = 是比较运算符,不是赋值   !=不等于  >大于   < 小于  >=  <=
    
    • 1

    查询所有未删除的数据【逻辑删除 deleted boolean default 0】

    select * from tb_users where deleted=0;
    
    select * from tb_users where deleted=1;
    
    • 1
    • 2
    • 3

    查询18岁以上的人

    	select * from tb_users where year(now())-year(birth)>18
    
    	逻辑运算   and与  or或   not非
    
    • 1
    • 2
    • 3

    查询18岁的男生

    select * from tb_useres where year(now())-year(birth)=18 and sex=1
    
    • 1

    查询1989出生的用户或者所有女性用户

    select * from tb_users where year(birth)=1989 or sex=0;
    
    • 1

    查询不是1989年出生的用户

    select * from tb_users where year(birth)!=1989
    
    select * from tb_users where not(year(birth)=1989)
    
    • 1
    • 2
    • 3

    针对null不能使用=或者!=进行判断

    查看口令为空的用户

    select * from tb_users where password is null;  -- 这里不能使用=null,否则返回为null
    
    	select * from tb_users where password is not null;
    
    • 1
    • 2
    • 3

    主要针对数值类型

    查询1989年到2000年出生的用户

    between 小值 and 大值   要求数据在指定的范围内,包括两端
    		not between..and 
    	
    	select * from tb_users where year(birth)>=1989 and year(birth)<=2000
    
    	
    	select * from t3 where id between 'ac' and 'ca';  也可以用于字符串类型,含义比较奇怪
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    主要针对字符串类型

    查询所有姓zhang的用户

    like 需要配合_或者%进行模糊查询  not like
    		_用于指代一个任意字符,%用于指代任意多个任意字符
    
    	select * from tb_users where username like 'zhang%'
    
    	查询名称中有国字的
    
    	select * from tb_users where username like '%国%';
    
    	查询张xx用户
    
    	select * from tb_users where usenrame like '张__'
    
    	组合  %张_  _张%
    
    	select * from tb_users where birth like '1989%';  在MySQL8中语法正确,birth是date类型
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    针对离散值的等值判断
    in或者not in

    查看1989、2004、1878、2020年出生的用户

    select * from tb_users where year(birth) in (1989,2004,1878,2020);
    
    	select * from tb_users where year(birth)=1989 or year(birth)=2004 or year(birth)=1878 or year(birth)=2020;
    
    • 1
    • 2
    • 3
  • 相关阅读:
    LeetCode每日一题(2311. Longest Binary Subsequence Less Than or Equal to K)
    如何使用gitee码云?创建库,克隆远程仓库,上传代码,小绿格等问题
    ROS program for SRI force sensor
    一、React基础知识
    C++学习day--23 枚举、类型定义、头文件
    经典面试题:重载和重写的区别
    通过事件绑定实现动画效果
    Cannot use @TaskAction annotation on method TransformTask.transform()
    关于 java 的动态绑定机制
    小程序如何设置下单提示语句
  • 原文地址:https://blog.csdn.net/qq_51222096/article/details/127911770
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号