SQL 高级查询

 查询没有地址的人(地址为null)
-- null值不能使用 = 比较
-- is是   is not不是
select * from stu where address is 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;
--查询性别 (distinct 去除重复值)
select distinct(sex) from stu;
--排序 order by(根据年龄,默认是升序)
-- 升序 asc
-- 降序 desc
select * from stu order by birthday desc;
-- 查看年龄大的前10人
select top 10 * from stu order by birthday;
-- 查看年龄大的前50%
-- percent 就是 %
select top 50 percent * from stu
order by birthday;
--------------------函数,方法
 
-- 查找特定字符的位置
select charindex('c','123a');
--名字里面带张的人
select * from stu where charindex('李',sname)!=0;
--len 字符的个数
select len('你好');
-- 查询学生表中所有的名字为两个字的
select * from stu where len(sname)=2;
--大写 upper 和小写 lower 函数
select upper('abc');
select lower('ABC');
-- 显示每个学生的名字和邮箱
-- 将邮箱大写
select sname,upper(email) from stu;
-- 清除空格
select ltrim('      你好');
select rtrim('你好      ');
-- 取字符
select right('你好啊',1);
select left('你好啊',1);
-- 取出学生表中姓为 李 的人
select * from stu where left(sname,1)='李';
-- 替换
select replace('aabbcc','bb','ff');
--将天津市变成经天市
update stu
set address=replace(address,'天津市','经天市');
---------------------------------
--当前日期
select getdate();
-- 增加日期
select dateadd(mm,3,getdate());
--求当前的55分钟之后是多久
select dateadd(mi,50,getdate());
--显示所有学生数据,在生日上面+100年
select sname,birthday,dateadd(yyyy,100,birthday)
from stu;
--日期相减
select dateadd(dd,-1,getdate());
--日期求差
select datediff(yyyy,'2020/1/1',getdate());
--显示每个学生的年龄
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());
--求1998年出生的学生
select * from stu where year(birthday)=2020;
----------------------------------
--绝对值
select abs(-1);
-- 求整
-- 1.01 -> 2,1
--向上
select ceiling(1.9999999999999999);
--向下
select floor(1.99999999999999999999);
--四舍五入
select round(1.3,0);
select floor(-1.9);
posted @   百变小新  阅读(143)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示