日期:2014-05-18  浏览次数:20561 次

两个数据表 连接查询 以及与Gridview的显示问题
我有两个表,一个是 课程信息表 C_ID, C_Style (课程编号,课程类型)
,另一个是 定课表,B_ID,C_ID,C_Style(定课id,课程编号,课程类型)

我用gridview绑定课程信息,用textbox对C_Style课程类型进行搜索。

我现在要实现的是gridview 搜索显示出来的信息为 在定课表中C_ID出现的次数小于4的 课程信息表中的相应信息。

请问各大神怎么实现?



------解决方案--------------------
SQL code

---->>TravyLee生成测试数据
if OBJECT_ID('Course')is not null
drop table Course
go
create table Course(
CouID int,
CouType varchar(10)
)
go
insert Course
select 1,'Java' union all
select 2,'SQL Server' union all
select 3,'HTML5'
if OBJECT_ID('CouBooked')is not null
drop table CouBooked
go
create table CouBooked(
BookedID int,
CouID int,
CouType varchar(10)--不明白为什么还要一个课程类型
)
go
insert CouBooked
select 1,1,'test1' union all
select 2,1,'test2' union all
select 3,3,'test3' union all
select 4,2,'test4' union all
select 5,1,'test5' union all
select 6,1,'test6' union all
select 7,2,'test7' union all
select 8,2,'test8' union all
select 9,2,'test9' union all
select 10,1,'test10' union all
select 11,3,'test11' union all
select 12,1,'test12'
go


select 
    CouID,CouType
from
    Course
where 
    CouID in(
select 
    CouID
from(
select 
    CouID,COUNT(CouID) as Times 
from 
    CouBooked 
group by 
    CouID
having COUNT(CouID)>=4
    )t
    )
/*
CouID    CouType
--------------------------
1    Java
2    SQL Server
*/