单表中的sql语句

--建立db_mydata数据库;
create database db_mydata;

--使用刚刚建立的db_mydata
use db_mydata;

--查询计算机与电子信息学院的同学
select* from 本科 where 学院='计算机与电子信息学院';
--查询某一列
select college,professional,class,studentid from 本科 ;

--在查询时使用别名
select college as 学院,
    professional as 专业,
    class as 班级,
    studentid as 学号
    from 本科;
   
--通过查询生成一个年龄字段
--使用datediff()函数计算年龄
select 牛卡级别,DATEDIFF(YYYY,出生年月,GETDATE()) as 年龄 from clientdata;

--简单的条件查询
select 资金账号,客户姓名,风险提示 from clientdata where 风险提示='未测评';

--不等于条件查询
select 资金账号,客户姓名,风险提示 from clientdata where 风险提示!='未测评';
select 资金账号,客户姓名,风险提示 from clientdata where 风险提示<>'未测评';

--大于小于条件查询
select 资金账号,客户姓名,风险提示,
DATEDIFF(yyyy,出生年月,GETDATE()) as 年龄
from clientdata where DATEDIFF(yyyy,出生年月,GETDATE())>23;

--查询数值区间
select 资金账号,客户姓名,风险提示,
DATEDIFF(yyyy,出生年月,GETDATE()) as 年龄
from clientdata where DATEDIFF(yyyy,出生年月,GETDATE())
between 24 and 50;

--and运算符查询
select 资金账号,客户姓名,风险提示,
DATEDIFF(yyyy,出生年月,GETDATE()) as 年龄
from clientdata where DATEDIFF(yyyy,出生年月,GETDATE()) between 1 and 23
and 风险提示='正常';


--like %模糊查询
select college,professional from
 regular where professional like '%科学%';
 
--使用[]通配符进行模糊查询姓 赵/周/秀
select * from regular where name like '[周赵秀]%';

--使用[]通配符进行模糊查询除姓 赵/周/秀的
select * from regular where name like '[^周赵秀]%';

--in 运算符条件查询(三个学院的毕业生)
select * from regular where college in('环境学院','计算机与电子信息学院','农学院');

--not in 运算符条件查询(除三个学院之外其他学院的毕业生信息  )
select * from regular where college not in('环境学院','计算机与电子信息学院','农学院');

--查询时去除重复记录
select distinct(college)from regular;

--group by
select COUNT(name)as 计数,college as 学院 from regular group by college;

--获取制定字段的空值
select * from clientdata where 风险类型 is null;

--查询前N条记录
select top 012 * from clientdata;

--查询数据的%率条数
select top 10 percent * from clientdata;

--单列排序asc升序,desc 降序
select * from clientdata order by 开户日期 asc;

--多列排序
select * from clientdata order by 开户日期,公司电话 asc;

原文地址:https://www.cnblogs.com/iomango/p/2793392.html