--2、查询工资超过2000快钱的员工 select [eId], [eName], [eSex], [eAge], [eSalary], [eDepId], [eintime] from employee where esalary >3000
--3、查询最新进来的5个员工 select Top 5 [eId], [eName], [eSex], [eAge], [eSalary], [eDepId], [eintime] from employee Orderby eintime desc
--4、查询员工的平均工资,最低工资 select'平均工资'as 工资情况,avg(esalary) as 金额 from employee unionall select'最低工资',min(esalary) from employee
--5、查询总共有多少员工 selectcount(*) as 员工总数 from employee
--6、查询每个部门有多少员工 select eDepId as 部门,count(*) as 人数 from employee groupby eDepId
--7、查询每个部门的平均工资 select eDepId as 部门,avg(eSalary) as 平均工资 from employee groupby eDepId
--8、查询每个部门男员工的平均工资 select eDepId as 部门,avg(eSalary) as 金额 from employee where eSex ='男'groupby eDepId
--9、查询平均工资超过2000的那些部门 select eDepId as 部门,avg(eSalary) as 金额 from employee groupby eDepId havingavg(eSalary) >2000 --
--10、查询员工工资(姓名,工资)的同时,同一结果集显示平均工资和最高工资 select ename as 姓名,esalary as 工资 from employee unionall --select avg(esalary) as 平均工资,max(esalary) as 最高工资 from employee select'平均工资',avg(esalary) from employee unionall select'最高工资',max(esalary) from employee
--11、查询名字里包含'定,月'的这些员工,并对结果按照年龄排序 select*from employee where ename like'%[定月]%'orderby eage desc
--输出所有数据中通话时间最长的5条记录。orderby datediff select top 5*from CallRecords orderby DATEDIFF(SECOND,StartDateTime,EndDateTime) desc
--输出所有数据中拨打长途号码(对方号码以0开头)的总时长。like、sum selectsum(DATEDIFF(SECOND,StartDateTime,EndDateTime)) as 总时长 from CallRecords where TelNum like'0%'
--输出本月通话总时长最多的前三个呼叫员的编号。
select top 3 callernumber as 呼叫员,sum(DATEDIFF(SECOND,StartDateTime,EndDateTime)) as 总时长 from CallRecords where datepart(MONTH,StartDateTime) =7 groupby CallerNumber orderbysum(DATEDIFF(SECOND,StartDateTime,EndDateTime)) desc
--输出本月拨打电话次数最多的前三个呼叫员的编号.group by,count(*) select top 3 callernumber as 呼叫员,count(*) as 打电话次数 from CallRecords where datepart(MONTH,StartDateTime) =7 groupby CallerNumber orderbycount(*) desc
using (SqlConnection con = new SqlConnection(conStr)) { using (SqlCommand cmd = con.CreateCommand()) { con.Open(); //string sql = "insert into score(studentid,english,math) values(200,300,300)"; //cmd.CommandText = sql; //int r = cmd.ExecuteNonQuery(); //Console.WriteLine($"查入了{r}条数据");
//string sql1 = "insert into score(studentid,english,math) values(300,400,400)"; //string sql1 = "update score set english = 114514 where studentid = 4"; //cmd.CommandText = sql1; //int r1 = cmd.ExecuteNonQuery(); //Console.WriteLine(r1);
string sql = "select count(*) from student"; cmd.CommandText = sql; int r2 = Convert.ToInt32(cmd.ExecuteScalar()); Console.WriteLine(r2); } } Console.ReadKey(); }
using (SqlConnection con = new SqlConnection(conStr)) { string sql = "select sid, sname, sgender, sage as '年龄',sbirthday from student"; using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); //读取表中所有数据 //sqlDataReader数据库读取对象 SqlDataReader sr = cmd.ExecuteReader(); //服务器端已经产生了数据 if (sr.HasRows) { //1、读取数据 //2、让指针向下移动 while (sr.Read()) { int idIndex = sr.GetOrdinal("sid");//GetOrdinal 根据列名获取列的索引 int id = Convert.ToInt32(sr[idIndex]); Console.WriteLine(id);
string name = sr[1].ToString(); Console.WriteLine(name);
--案例1:查询所有学生的姓名、年龄及所在班级 select sname as'姓名',sage as'年龄',cname as'班级' from student as a innerjoin class as b on a.sclassid = b.cid --案例2:查询年龄超过20岁的学生的姓名、年龄及所在班级 select sname as'姓名',sage as'年龄',cname as'班级' from student as a innerjoin class as b on a.sclassid = b.cid where a.sage <20 --案例3:查询学生姓名、年龄、班级及成绩(三表联合查询) select a.sname as'姓名',a.sage as'年龄',b.cname as'班级',c.english as'英语成绩' from student as a innerjoin class as b on a.sclassid = b.cid innerjoin score as c on a.sid = c.studentid --案例4:查询所有学生(参加及未参加考试的都算)及成绩 select a.sname as'学生姓名',c.english as'英语成绩', case c.math when c.math then c.math else0 endas'数学成绩' from student as a innerjoin class as b on a.sclassid = b.cid innerjoin score as c on a.sid = c.studentid
--让不及格的人数不到总人数的一般 if exists(select*from sys.objects where name ='usp_UpdateScore') drop proc usp_updateScore go create proc usp_UpdateScore --参数 --不需要接declare @englishpassint, @mathpassint as --声明一个变量,存储不及格的人数 declare@failedCountint --给不及格的人数赋值 select@failedCount=count(*) from score where english <@englishpassand math <@mathpass declare@totalCountint --给总人数赋值 select@totalCount=count(*) from score while(1=1) begin if(@failedCount>@totalCount/2) begin update score set english = english +2,math = math +2 select@failedCount=count(*) from score where english <@englishpassand math <@mathpass end else begin break end end