student
id name
1 小王
2 小李
sendmessage
studentid issend
1 0
1 0
--------------------
显示:
studentid name issend studentid出现次数
1 小王 0 2
1 小王 0 2
7 个解决方案
#1
select a.*,b.issend,(select count(1) from sendmessage where studentid=a.id)
from student a inner join sendmessage b on a.id=b.studentid
#2
select s.*,m.issend
,(select count(1) from sendmessage where studentid =m.studentid ) as [studentid出现次数
]
from student s,sendmessage m
where s.id = m.studentid
#3
select
a.*,b.issend,c.num
from
student a,
sendmessage b,
(select studentid,count(1) as num from sendmessage group by studentid)c
where
a.id=b.studentid
and
a.id=c.studentid
#4
select sm.studentid,st.name,issend,count(studentid)
from student st,sendmessage sm
where st.id=sm.studentid
group by sm.studentid,st.name,issend
#5
select sm.studentid,st.name,issend,(select count(1) from sendmessage where studentid =sm.studentid )
from student st,sendmessage sm
where st.id=sm.studentid
group by sm.studentid,st.name,issend
from student st,sendmessage sm
where st.id=sm.studentid
group by sm.studentid,st.name,issend
#6
2005
--> 测试数据:[student]
if object_id('[student]') is not null drop table [student]
go
create table [student]([id] int,[name] varchar(4))
insert [student]
select 1,'小王' union all
select 2,'小李'
--> 测试数据:[sendmessage]
if object_id('[sendmessage]') is not null drop table [sendmessage]
go
create table [sendmessage]([studentid] int,[issend] int)
insert [sendmessage]
select 1,0 union all
select 1,0
--------------------------------查询开始------------------------------
select b.*,a.[issend],出现次数=count(1) over() from [sendmessage] a join [student] b on a.studentid=b.id
/*
id name issend 出现次数
----------- ---- ----------- -----------
1 小王 0 2
1 小王 0 2
(2 行受影响)
*/
#7
楼主结贴好快,值得表扬
#1
select a.*,b.issend,(select count(1) from sendmessage where studentid=a.id)
from student a inner join sendmessage b on a.id=b.studentid
#2
select s.*,m.issend
,(select count(1) from sendmessage where studentid =m.studentid ) as [studentid出现次数
]
from student s,sendmessage m
where s.id = m.studentid
#3
select
a.*,b.issend,c.num
from
student a,
sendmessage b,
(select studentid,count(1) as num from sendmessage group by studentid)c
where
a.id=b.studentid
and
a.id=c.studentid
#4
select sm.studentid,st.name,issend,count(studentid)
from student st,sendmessage sm
where st.id=sm.studentid
group by sm.studentid,st.name,issend
#5
select sm.studentid,st.name,issend,(select count(1) from sendmessage where studentid =sm.studentid )
from student st,sendmessage sm
where st.id=sm.studentid
group by sm.studentid,st.name,issend
from student st,sendmessage sm
where st.id=sm.studentid
group by sm.studentid,st.name,issend
#6
2005
--> 测试数据:[student]
if object_id('[student]') is not null drop table [student]
go
create table [student]([id] int,[name] varchar(4))
insert [student]
select 1,'小王' union all
select 2,'小李'
--> 测试数据:[sendmessage]
if object_id('[sendmessage]') is not null drop table [sendmessage]
go
create table [sendmessage]([studentid] int,[issend] int)
insert [sendmessage]
select 1,0 union all
select 1,0
--------------------------------查询开始------------------------------
select b.*,a.[issend],出现次数=count(1) over() from [sendmessage] a join [student] b on a.studentid=b.id
/*
id name issend 出现次数
----------- ---- ----------- -----------
1 小王 0 2
1 小王 0 2
(2 行受影响)
*/
#7
楼主结贴好快,值得表扬