
建立一个测试goods表
create table goods (
id CHAR(36) default (UUID()) key ,
name VARCHAR(10),
price DECIMAL(5,1),
num int
)
以下这些用法全部用于where之后的操作
值的判读
大于 小于 大于等于 小于等于 不等于 的应用 and连接语句进行筛选
#查询num等于一个固定数的语句
select * from goods where num='10'
#查询价格大于700的商品
select * from goods where price >'700'
#查询价格小于等于666.3的商品
select * from goods where price<'666.3'
#查询价格大于等于666.3的商品
select * from goods where price>='666.3'
#查询价格小于等于666.3的商品
select * from goods where price<='666.3'
#查询价格不是666.3的商品
select * from goods where price!='666.3'
select * from goods where price<>'666.3'
#多条件 and链接
select * from goods where price > '500' and price < '1000'
#举例说明
#查询2月6号注册的用户
select * from goods where creatime> '2023-02-06 00:00:00 'and creatime <'2023-02-06 23:59:59'
#使用格式化日期进行输出
select * from goods where DATE_FORMAT(creatime ,"%Y-%m-%d")='2023-02-06'
真假条件(以后再写)
NULL运算( is null , is not null , '')
#判断good表中tags是不是含有空值
select * from good where tags is null
#判断good表中tags是不是含有非空值
select * from good where tags is not null
null和空字符串的意义不一样 。空字符串是有值的,null是没有值,不存在
范围(in)适用于范围比较小的查询 in / not in
可以查询字符串也可以查询表达式
select * from cen
#查询id 为1 2 3的 数据
select * from cen where id >=1 and id <=3
#查询id 为1 3 5 的 数据 空格并不影响他的操作,这里没有单引号
select * from cen where id in (1 ,3 ,5 )
#查找id为这两个字符串的数据
select * from goods where id in ('d6823ce8-a6c8-11ed-887a-00ff4dfddb6c','d682547a-a6c8-11ed-887a-00ff4dfddb6c')
instr 相当于查找子字符串的位置
instr函数返回要截取的字符串在源字符串中的位置,而且只检索一次
instr单独使用的时候不用放于where之后
INSTR(str,substr) str和substr分别为源字符串和子字符串
instr(str,substr)>0相当于 字段 like ‘%关键字%’
instr(字段,‘关键字’)=1相当于 字段like ‘关键字%’
instr(字段,‘关键字’)=0相当于 字段not like ‘%关键字%’
select * from 表名 where instr(字段名,#{catName})>0
# instr 的应用
select instr( "hello","ll" ),INSTR("hello","e"),INSTR("hello","o")
#将n个字符串整合为一个字符串并进行数据判断
select * from goods where INSTR('d6823ce8-a6c8-11ed-887a-00ff4dfddb6c,d682547a-a6c8-11ed-887a-00ff4dfddb6c',id )>0


between and 和 not between and 用于数据和日期
#数据id的查询
select * from cen where id between 1 and 3
#查找固定范围内的日期
select * from goods where creatime between '2023-02-05 00:00:00' and '2023-02-05 23:59:59'
DATE_SUB()函数从DATE或DATETIME值中减去时间值(或间隔)。 下面说明了DATE_SUB()函数的语法:
#直接使用select进行查询
select DATE_SUB(NOW(),interval 7 day) lowtime
#查询过去一周的注册信息(过去)
#格式化创建时间,
SELECT * from os where DATE_FORMAT(creatime,"%Y-%m-%d")>=DATE_SUB(CURRENT_TIMESTAMP ,INTERVAL 7 DAY)
#查询近三天注册的会员(现在)(过去)
select * from os where DATE_FORMAT(NOW(),"%Y-%m-%d")>=DATE_SUB(CURRENT_DATE,INTERVAL 3 DAY)
#查询近三天要过期的会员(未来)
select * from os where endtime between now() and date_add(now(),interval 3 day)
#将时间加到需要过期的时候,在和结束的时间比较
select * from os where DATE_ADD(NOW(),INTERVAL 3 day) >endtime
#结束的时间减去三天在和当前的时间进行比较
select * from os where DATE_SUB(endtime, interval 3 day)> CURRENT_DATE
#查询2.3到2.5注册的人的信息(现在)(过去)
select * from os where DATE_FORMAT(creatime,"%Y-%m-%d") between '2023-02-05' and '2023-02-07'
模糊查询
使用like 关键字或者_ 一个_代表一个字符长度 length(1)指的是一个字节长度 汉字在 CHARSET=utf8mb3 中代表三个字节
length说的是字节的长度,一个汉字占三个字节而一个英文字母只占一个字节
在这个里边双引号和单引号都行的,但是又什么区别呢????
#查询张开头的name
select * from os where name like "张%"
#查询含有张的字
select * from os where name like "%张%"
#查询含有张的字并且长度为三
#为什么查询不出来
select * from os where name like "张%" and LENGTH(name)=3
#查询含有张的字并且长度为三
#为什么查询不出来
#一个汉字占三个字节 CHARSET=utf8mb3 length说的是字节长度
select * from os where name like "张%" and LENGTH(name)=9
#一个_占一个字符
select * from os where name like "张__"
select * from os where name like "a__"
#三个下滑线,代表三个字符
select * from os where name like "___"
正则表达式(regrxp)
在正则表达式中.(点代表一个任意的符号),*代表前边的符号出现0次或者多次
#查找名字含有张的数据
select * from os where name REGEXP '[.]*张[.]*'
#在表达式中间的空格也算一个标识符
#查找名字含有a的数据
select * from os where name REGEXP '\.*a\.*'
#查找名字含有a开头的数据
select * from os where name REGEXP '^a\.*'