首页 > 数据库 >【Mysql】 数据库高级查询

【Mysql】 数据库高级查询

时间:2022-10-13 17:05:51浏览次数:41  
标签:name students 18 数据库 查询 Mysql where age select


 

# 查询

# 查询所有字段
# select * from 表名
select * from students;
select * from class;


# 查询指定字段
# select 列1,列2,..... from 表名
select name,age from students;

# 使用as给字段起别名
# select 字段 as 名字 ...... from 表名
select name as 姓名,age as 年龄 from students;

# select 表名.字段, ...... from 表名
select students.name,students.age from students;

# 可以通过as给表起别名
# select 别名.字段 ...... from 表名 as 别名;
select s.name,s.age from students as s;

# 查询数据去重
select distinct gender from students;


# 条件查询
# 比较运算符
# >
# 查询大于18岁的信息
select * from students where age>18;
# <
# 查询小于18岁的信息
select * from students where age<18;
# >=
# 查询大于18岁的信息
select * from students where age>=18;
# <=
# 查询大于18岁的信息
select * from students where age<=18;
# =
# 查询大于18岁的信息
select * from students where age=18;
# !=
# 查询不等于18岁的信息
select * from students where age!=18;

# 逻辑运算符
# and
# 18到28之间的所有学生信息
select * from students where age>18 and age<28;
# 查询18岁以上的女性
select * from students where age>18 and gender="女";
select * from students where age>18 and gender=2;
# or
# 18岁以上或者身高高过180以上
select * from students where age>18 or height>180;
# not
# 不在18岁以上的女性这个范围内的信息
select * from students where not (age>18 and age<28);
# 年龄不是小于或者等于18 ,并且是女性
select * from students where not age<=18 and gender=2;

# 模糊查询
# 查询姓名为小的数据信息
select name from students where name="小";
# like 效率较低
# % 替换0个或者多个
# 查询姓名中以"小"开始所有的名字
select name from students where name like "小%";
# _ 替换一个
# 查询姓名中以"小"开始的两个字的名字
select name from students where name like "小_";
# 查询姓名中包含有小的所有名字
select name from students where name like "%小%";
# 查询有两个字的名字
select name from students where name like "__";
# 查询有三个字的名字
select name from students where name like "___";
# 查询至少有两个字的名字
select name from students where name like "__%";
# rlike 正则
# 查询以周开始的姓名
select name from students where name rlike "^周.*";
# 查询以周开始以伦结束的姓名
select name from students where name rlike "^周.*伦$";


# 范围查询
# in {1,2,8} 表示在一个非连续的范围内
# 查询18,34的姓名
select name from students where age=18 or age=34;
select name from students where age in {18,34,45};
# not in 不在连续的范围之内
# 年龄不在18,34岁之间的信息
select name from students where age not in {18,34};

# between...and... 表示在一个连续的范围之内
select name,age from students where age between 18 and 34;
# 查询 年龄不在18到34之间的信息
select name from students where age not between 18 and 34;

# 空判断
# 判空 is null
# 查询身高为空的信息
select * from students where heights is null;
select * from students where heights is Null;
select * from students where heights is NULL;
# 判非空is not null
select * from students where heights is not null;

 

标签:name,students,18,数据库,查询,Mysql,where,age,select
From: https://blog.51cto.com/u_15828115/5754009

相关文章

  • 【Mysql】mysql 数据库的增删改查
    #进入数据库mysql-u[user]-pmysql-u[user]-p[passwd]#举例mysql-uroot-pmysql-uroot-pmysql#退出数据库exitquit#ctrl+d也可以退出数据库#显示当前数据库的......
  • DQL基础查询
    createTABLEstudent1(idINT,--编号NAMEVARCHAR(20),--姓名ageINT,--年龄sexVARCHAR(5),--性别addressVARCHAR(100),--地址mathINT,--数学eng......
  • 数据库改造方案 | 同花顺、弘源泰平真实案例分享
    在金融量化交易场景中,每天都会产生大量的交易记录和交易信息需要存储,同时对数据也有较高要求的查询需求,整体需求概括起来就是历史数据的存储、实时数据的接收以及数据的监......
  • mysql数据同步
    Navicat同步数据库中数据kettle实现mysql单表增量同步使用Kettle同步mysql数据,增量同步教程执行步骤Navicat定时同步数据库使用Kettle进行数据同步(增量)KETTLE安装及连接M......
  • MySQL基础架构及一条SQL查询语句是如何执行的
    MySQL基础架构及一条SQL查询语句是如何执行的该文摘抄自林晓斌老师的文章MySQL的基本架构示意图,从中你可以清楚地看到SQL语句在MySQL的各个功能模块中的执行过程......
  • mysql开启访问数据权限
    赋予任何主机访问数据的权限GRANTALLPRIVILEGESON*.*TO'root'@'%'WITHGRANTOPTION修改生效FLUSHPRIVILEGES为了测试部署程序的保存库问题......
  • MySQL数据库中事务默认自动提交
    事务提交的两种方式自动提交mysql就是自动提交的一条DML(增删改) 语句会自动提交一次事务手动提交Oracle 数据库默认是手......
  • 达梦dba_segments指定表名查询到的大小都包含哪些数据
    一、结论dba_segments指定表名查询到的段大小包含索引、约束、表字段数据(包含LOB字段)(1)表(不包含LOB字段)创建默认分配2个簇,1个簇用于存放表结构及字段数据,1个簇用于存放clus......
  • JDBC各个类详解-DriverManager-注册驱动、DriverManager-获取数据库连接
    JDBC各个类详解-DriverManager-注册驱动riverManager:驱动管理对象功能:1.注册驱动:告诉程序该使用哪一个数据库驱动jarstatic void registerD......
  • Oracle数据库下的DDL、DML、DQL、TCL、DCL
    首发微信公众号:SQL数据库运维原文链接:https://mp.weixin.qq.com/s?__biz=MzI1NTQyNzg3MQ==&mid=2247485212&idx=1&sn=450e9e94fa709b5eeff0de371c62072b&chksm=ea37536cdd......