论坛里看到的一个SQL问题及解答
问题:
有一个分数表
id classid,score
1 01 120
2 01 128
3 02 98
4 04 134
5 04 78
现在要统计 各班score >120,和大于90分的人数
达到如下效果
classid >120 >90
01 10 29
02 9 32
03 0 20
答案:
select
classid,
sum
(
case
when
score
>
120
then
1
else
0
end
)
as
[
>120
]
,
sum
(
case
when
score
>
90 and score <=120
then
1
else
0
end
)
as
[
>90
]
from
tb
group
by
classid
相关文档:
/*
use master
go
if DB_ID('UserImage') is not null
drop database UserImage
create database UserImage
go
use UserImage
go
create table Images
(
Image_Name nvarchar(255) primary key,
Image_Data Image not null
)
go
create proc InsertImage
(
@Image_Name nv ......
SQL语句优化:
(1)可以过滤掉最大数量记录的条件必须写在WHERE子句的末尾.
(2)SELECT子句中避免使用 ‘ * ‘
(3)减少访问数据库的次数
方法3 (高效)?
SELECT A.EMP_NAME , A.SALARY , A.GRADE,
B.EMP_NAME , B.SALARY , B.GRADE
from EMP A,EMP B
WHERE A.EMP_NO = 342
AND B.EMP_NO = 291;
( ......
查询 card 的记录为两次以上的 card,记录数:
select count(card), card from TableName group by card having count(card) > 1
级联更新,级联删除:
ColumnName type not null constraint FK_Name foreign key(ColumnName) references PrimaryTable(ColumnName) on update/delete cascade
自动计算列:
create tabl ......
最先希望使用datatable.columns种所含有的属性直接获取相关信息,可测试后发现,除了字段名和字段类型,其他数据一概找不到。
GOOGLE网上很多种方法,其中最直接的是直接查询SQLSERVER的information_schema.colums视图,从中直接获取信息,不过后来又发现,虽然数据时全了,可是需要通过很复杂的方法(sys.all_columns sys ......
问题来自http://topic.csdn.net/u/20091128/10/6750afae-71df-42df-ae23-14024736e1d8.html?64174
依我猜测可能是要拼字段名称
if (D > 0)
{
UpData_D = "D_D" + D.ToString();
DataSet myData = Socut.Data.ExecuteDataSet("UPDATE YUAN SET " + UpData_D + "='" + Y + "',..........
}
......