加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SqlServer中行变列

发布时间:2020-12-12 15:26:52 所属栏目:MsSql教程 来源:网络整理
导读:???? 例如创建一个表 ???????????????? create table stz ????????????? ( ???????????? stuNO int? primary key, ???????????? subject nvarchar(20) not null, ???????????? score int not null ????????????? ) ????????????? insert into stz values(100

???? 例如创建一个表

???????????????? create table stz

????????????? (

???????????? stuNO int? primary key,

???????????? subject nvarchar(20) not null,

???????????? score int not null

????????????? )

????????????? insert into stz values(1000,'数学',90)

????????????? insert into stz values(1001,'语文',90)

????????????? insert into stz values(1002,'英语',90)

?

????????????? insert into stz values(1001,90)

????????????? insert into stz values(1000,90)

?

????????????? insert into stz values(1002,90)

--????????????? 现在的情况是

??????????? select * from stz;

????????????????????????????????? :学号,subject,score

??????????? 我们要把它变成: 学号 ,数学,语文,英语

????????????实现如下:

?????????? select stuNo,

?????????????????????????? 数学=(select sum(score) from stz where subject='数学' and stuNO=shi.stuNo group by stuNO),

????????????????????????? ?语文=(select sum(score) from? stz where subject='语文' and stuNO=shi.stuNO group by stuNo),

?????????????????????????? 英语=(select? sum(score) from stz where subject='英语' and stuno=shi.stuNo group by stuNO)

????????? from stz as shi group by stuNo;

???????????---------------------结束

???????????1,突然想到一些问题,如果我们在多表连接查询时为了性能我们先考虑连接查询,子查询,嵌套查询,知道为什么吗?还有在写查询语句是尽量不要在where 后写运算逻辑?

?????????? 其实这里只有一个共同的原因:嵌套查询,where ,后的条件在你查每条数据时都会运行,很是影响性能。

?????????? 2, 看下这个查语句,有问题吗?select * from (select * from stz);

????????????????? 应改成select * from (select * from stz ) as shi? 必须把那个表集合付给一个变量的。

?????????? 3,看下这段C#代码?会输出什么

?????????????????? int i=2000;

?????????????????? object o=i;

????????????????????i=2001;

??????????????????? int j=(int)o;

????????????????? console.writeLine("{i},{0},{j}");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读