这里我举例说明我的需求,实际情况字段要比下面的多好多。人员表:t_personpersonid int, //主键,人员ID
name varchar(32) //人员姓名
科目一考试表:t_km1id int, //主键,自增
personid int,//人员ID
status tinyint,//考试状态 0:未通过 1:通过
examdate datetime//考试日期
科目二考试表:t_km2id int, //主键,自增
personid int,//人员ID
status tinyint,//考试状态 0:未通过 1:通过
examdate datetime//考试日期
SQL语句的功能是:将科目一和科目二考试已经通过的人过滤掉,显示出那些有一科未通过或有两科未通过的人员。
我的SQL语句是这样写的:select * from v_km12 a where not exists (select personid from t_km1 where personid=a.personid and status=1 and exists (select personid from t_km2 where personid=a.personid and status=1))
其中v_km12是对以上三个表建立的视图,视图的内容是显示人员名称和最后一次的考试时间、考试成绩。视图没有问题。
name varchar(32) //人员姓名
科目一考试表:t_km1id int, //主键,自增
personid int,//人员ID
status tinyint,//考试状态 0:未通过 1:通过
examdate datetime//考试日期
科目二考试表:t_km2id int, //主键,自增
personid int,//人员ID
status tinyint,//考试状态 0:未通过 1:通过
examdate datetime//考试日期
SQL语句的功能是:将科目一和科目二考试已经通过的人过滤掉,显示出那些有一科未通过或有两科未通过的人员。
我的SQL语句是这样写的:select * from v_km12 a where not exists (select personid from t_km1 where personid=a.personid and status=1 and exists (select personid from t_km2 where personid=a.personid and status=1))
其中v_km12是对以上三个表建立的视图,视图的内容是显示人员名称和最后一次的考试时间、考试成绩。视图没有问题。
你试试这个:with table as
(
select personid from t_km1
where status = 0
union
select personid from t_km2
where status = 0
)
select *
from t_Person
inner join table on table.personid = t_Person.personid
如果自己改用数组,或vector\list的话,速度提高10倍是可能的。
前提是,数据不复杂,数据库结构不经常改动。否则编程起来麻烦很多。
你试试这个:with table as
(
select personid from t_km1
where status = 0
union
select personid from t_km2
where status = 0
)
select *
from t_Person
inner join table on table.personid = t_Person.personid
服务器: 消息 156,级别 15,状态 1,行 1
在关键字 'with' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 11
在关键字 'table' 附近有语法错误。
而且我发现一个问题,只要我的SQL语句中使用了视图就直接慢十倍不至,搞不清为什么。