SQL 高级查询
查询没有地址的人(地址为null)
-- null值不能使用 = 比较
-- is是 is not不是
select * from stu where address is null;
-- null值不能使用 = 比较
-- is是 is not不是
select * from stu where address is null;
--查询有邮箱的
select * from stu where email is not null;
select * from stu where email is not null;
--查询有家有邮箱的
--java中多个条件用或者和并且
--sql中没有特殊符号 or或者 and并且
select * from stu
where address is not null and email is not null;
--java中多个条件用或者和并且
--sql中没有特殊符号 or或者 and并且
select * from stu
where address is not null and email is not null;
--查询性别 (distinct 去除重复值)
select distinct(sex) from stu;
select distinct(sex) from stu;
--排序 order by(根据年龄,默认是升序)
-- 升序 asc
-- 降序 desc
-- 升序 asc
-- 降序 desc
select * from stu order by birthday desc;
-- 查看年龄大的前10人
select top 10 * from stu order by birthday;
-- 查看年龄大的前50%
-- percent 就是 %
-- percent 就是 %
select top 50 percent * from stu
order by birthday;
order by birthday;
--------------------函数,方法
-- 查找特定字符的位置
select charindex('c','123a');
-- 查找特定字符的位置
select charindex('c','123a');
--名字里面带张的人
select * from stu where charindex('李',sname)!=0;
select * from stu where charindex('李',sname)!=0;
--len 字符的个数
select len('你好');
select len('你好');
-- 查询学生表中所有的名字为两个字的
select * from stu where len(sname)=2;
select * from stu where len(sname)=2;
--大写 upper 和小写 lower 函数
select upper('abc');
select lower('ABC');
select upper('abc');
select lower('ABC');
-- 显示每个学生的名字和邮箱
-- 将邮箱大写
select sname,upper(email) from stu;
-- 将邮箱大写
select sname,upper(email) from stu;
-- 清除空格
select ltrim(' 你好');
select rtrim('你好 ');
select ltrim(' 你好');
select rtrim('你好 ');
-- 取字符
select right('你好啊',1);
select left('你好啊',1);
select right('你好啊',1);
select left('你好啊',1);
-- 取出学生表中姓为 李 的人
select * from stu where left(sname,1)='李';
select * from stu where left(sname,1)='李';
-- 替换
select replace('aabbcc','bb','ff');
select replace('aabbcc','bb','ff');
--将天津市变成经天市
update stu
set address=replace(address,'天津市','经天市');
update stu
set address=replace(address,'天津市','经天市');
---------------------------------
--当前日期
select getdate();
select getdate();
-- 增加日期
select dateadd(mm,3,getdate());
select dateadd(mm,3,getdate());
--求当前的55分钟之后是多久
select dateadd(mi,50,getdate());
select dateadd(mi,50,getdate());
--显示所有学生数据,在生日上面+100年
select sname,birthday,dateadd(yyyy,100,birthday)
from stu;
select sname,birthday,dateadd(yyyy,100,birthday)
from stu;
--日期相减
select dateadd(dd,-1,getdate());
select dateadd(dd,-1,getdate());
--日期求差
select datediff(yyyy,'2020/1/1',getdate());
select datediff(yyyy,'2020/1/1',getdate());
--显示每个学生的年龄
select sname,birthday,
datediff(yyyy,birthday,getdate()),
datediff(dd,birthday,getdate())
from stu;
select sname,birthday,
datediff(yyyy,birthday,getdate()),
datediff(dd,birthday,getdate())
from stu;
-- year年
-- month 月
-- day 日
select year(getdate());
select month(getdate());
select day(getdate());
-- month 月
-- day 日
select year(getdate());
select month(getdate());
select day(getdate());
--求1998年出生的学生
select * from stu where year(birthday)=2020;
select * from stu where year(birthday)=2020;
----------------------------------
--绝对值
select abs(-1);
select abs(-1);
-- 求整
-- 1.01 -> 2,1
-- 1.01 -> 2,1
--向上
select ceiling(1.9999999999999999);
--向下
select floor(1.99999999999999999999);
--四舍五入
select round(1.3,0);
select ceiling(1.9999999999999999);
--向下
select floor(1.99999999999999999999);
--四舍五入
select round(1.3,0);
select floor(-1.9);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构