联合查询 问题

lxyd000 2010-02-04 11:08:16
exam 表

examId examName examPrice beginTime endTime
1 j2SE 20 2008-05-04 2008-08-20
2 j2EE 40 2008-02-15 2008-7-05
3 j2ME 50 2008-01-18 2008-3-30
4 C 30 2009-05-20 2009-08-20
5 C++ 50 2009-03-22 2009-08-29
6 C# 40 2009-12-20 2010-08-20

exam_user 表
euId userId examId
1 张三 1
2 张三 2
3 张三 3
4 李四 1
5 李四 2
6 李四 3
7 王五 1
8 王五 4
9 王五 3
10 张三 6
11 王五 5

我现在想查出来的结果是
examName countPerson(exam_user 表中查出的)
j2SE 3
C++ 1
C# 1
j2ME 3
j2EE 2
也就是说 查看 exam 表的时候顺便 把 examId 在 exam_user 表中出现的次数也作为一列反回去。我现在用的ms sql 写了一句: select e.examName, (select count(*) from exam_user where eu.examId=e.examId) from exam e 可以通过也是想要的结果。但是我想 限制一下 countPerson 大于 X 个值的显示出来 就像: countPerson >=2 时:
examName countPerson
j2SE 3
j2ME 3
j2EE 2
请问哪位指点指点 谢了。(对了 我用的是 Hibernater)
...全文
200 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaITOldMan 2010-02-09
  • 打赏
  • 举报
回复
SELECT A.examName,COUNT(1) AS countPerson
FROM exam A
JOIN exam_user B
ON A.examId=B.examId
HAVING COUNT(1)>=2
lovezx1028 2010-02-08
  • 打赏
  • 举报
回复
//
a43402614 2010-02-08
  • 打赏
  • 举报
回复

create table exam(
examid int,
examname varchar(20),
examPrice int,
begin_time datetime,
end_time datetime
)

create table exam_user(
euId int,
userId varchar(20),
examId int
)


insert exam
select 1,'j2SE',20,'2008-05-04','2008-08-20' union all
select 2,'j2EE',40,'2008-02-15','2008-7-05' union all
select 3,'j2ME',50,'2008-01-18','2008-3-30' union all
select 4,'C',30,'2009-05-20','2009-08-20' union all
select 5,'C++',50,'2009-03-22','2009-08-29' union all
select 6,'C#',40,'2009-12-20','2010-08-20'

insert exam_user
select 1,'张三',1 union all
select 2,'张三',2 union all
select 3,'张三',3 union all
select 4,'李四',1 union all
select 5,'李四',2 union all
select 6,'李四',3 union all
select 7,'王五',1 union all
select 8,'王五',4 union all
select 9,'王五',3 union all
select 10,'张三',6 union all
select 11,'王五',5

select * from exam
select * from exam_user

select A.examname,COUNT(*) countPerson from (select examname from exam inner join exam_user on exam.examid=exam_user.examId) A
group by A.examname
-------------------
examname countPerson
C 1
C# 1
C++ 1
j2EE 2
j2ME 3
j2SE 3

lxyd000 2010-02-05
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 bancxc 的回复:]
SQL code--借用下数据select exam.*,t.countpersonfrom examjoin
(select examId,count(*)as countpersonfrom exam_usergroupby examIdhavingcount(*)>=2
) ton exam.examid=t.examId/*
examId examName examPrice beginTime endTime countperson
----------- -------- ----------- ----------------------- ----------------------- -----------
1 j2SE 20 2008-05-04 00:00:00.000 2008-08-20 00:00:00.000 3
2 j2EE 40 2008-02-15 00:00:00.000 2008-07-05 00:00:00.000 2
3 j2ME 50 2008-01-18 00:00:00.000 2008-03-30 00:00:00.000 3

(3 行受影响)*/
[/Quote]
放到 Hibernater 里能行吗? 我怎么出不来(Hibernater)
lxyd000 2010-02-04
  • 打赏
  • 举报
回复
谢谢各位好心人。
SELECT examName,count(1) from
exam a INNER JOIN exam_user b
ON a.examid=b.examid
GROUP BY examName
HAVING count(1)>2
请问 COUNT(1) 统计的是 哪列啊? 怎么理解呢!
jack15850798154 2010-02-04
  • 打赏
  • 举报
回复
路过,看看。。
bancxc 2010-02-04
  • 打赏
  • 举报
回复
--借用下数据
select exam.*,t.countperson
from exam
join
(
select examId,count(*) as countperson
from exam_user
group by examId
having count(*) >=2
) t on exam.examid=t.examId

/*
examId examName examPrice beginTime endTime countperson
----------- -------- ----------- ----------------------- ----------------------- -----------
1 j2SE 20 2008-05-04 00:00:00.000 2008-08-20 00:00:00.000 3
2 j2EE 40 2008-02-15 00:00:00.000 2008-07-05 00:00:00.000 2
3 j2ME 50 2008-01-18 00:00:00.000 2008-03-30 00:00:00.000 3

(3 行受影响)

*/
bancxc 2010-02-04
  • 打赏
  • 举报
回复
--借用下数据
select exam.*,t.countperson
from exam
join
(
select examId,count(*) as countperson
from exam_user
group by examId
) t on exam.examid=t.examId and t.countperson>=2

/*
examId examName examPrice beginTime endTime countperson
----------- -------- ----------- ----------------------- ----------------------- -----------
1 j2SE 20 2008-05-04 00:00:00.000 2008-08-20 00:00:00.000 3
2 j2EE 40 2008-02-15 00:00:00.000 2008-07-05 00:00:00.000 2
3 j2ME 50 2008-01-18 00:00:00.000 2008-03-30 00:00:00.000 3

(3 行受影响)

*/
--小F-- 2010-02-04
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2010-02-04 11:15:24
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[exam]
if object_id('[exam]') is not null drop table [exam]
go
create table [exam]([examId] int,[examName] varchar(4),[examPrice] int,[beginTime] datetime,[endTime] datetime)
insert [exam]
select 1,'j2SE',20,'2008-05-04','2008-08-20' union all
select 2,'j2EE',40,'2008-02-15','2008-7-05' union all
select 3,'j2ME',50,'2008-01-18','2008-3-30' union all
select 4,'C',30,'2009-05-20','2009-08-20' union all
select 5,'C++',50,'2009-03-22','2009-08-29' union all
select 6,'C#',40,'2009-12-20','2010-08-20'
--> 测试数据:[exam_user]
if object_id('[exam_user]') is not null drop table [exam_user]
go
create table [exam_user]([euId] int,[userId] varchar(4),[examId] int)
insert [exam_user]
select 1,'张三',1 union all
select 2,'张三',2 union all
select 3,'张三',3 union all
select 4,'李四',1 union all
select 5,'李四',2 union all
select 6,'李四',3 union all
select 7,'王五',1 union all
select 8,'王五',4 union all
select 9,'王五',3 union all
select 10,'张三',6 union all
select 11,'王五',5
--------------开始查询--------------------------
select
examName,count(1) as countPerson
from
exam a
left join exam_user b
on
a.examid=b.examid
group by
examName
having count(1)>=2
----------------结果----------------------------
/*examName countPerson
-------- -----------
j2EE 2
j2ME 3
j2SE 3

(3 行受影响)
*/
百年树人 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 josy 的回复:]
TRY
SQL codeSELECT A.examName,COUNT(1)AS countPersonFROM exam AJOIN exam_user BON A.examId=B.examIdHAVINGCOUNT(1)>=2
[/Quote]
---测试数据---
if object_id('[exam]') is not null drop table [exam]
go
create table [exam]([examId] int,[examName] varchar(4),[examPrice] int,[beginTime] datetime,[endTime] datetime)
insert [exam]
select 1,'j2SE',20,'2008-05-04','2008-08-20' union all
select 2,'j2EE',40,'2008-02-15','2008-7-05' union all
select 3,'j2ME',50,'2008-01-18','2008-3-30' union all
select 4,'C',30,'2009-05-20','2009-08-20' union all
select 5,'C++',50,'2009-03-22','2009-08-29' union all
select 6,'C#',40,'2009-12-20','2010-08-20'
if object_id('[exam_user]') is not null drop table [exam_user]
go
create table [exam_user]([euId] int,[userId] varchar(4),[examId] int)
insert [exam_user]
select 1,'张三',1 union all
select 2,'张三',2 union all
select 3,'张三',3 union all
select 4,'李四',1 union all
select 5,'李四',2 union all
select 6,'李四',3 union all
select 7,'王五',1 union all
select 8,'王五',4 union all
select 9,'王五',3 union all
select 10,'张三',6 union all
select 11,'王五',5

---查询---
SELECT A.examName,COUNT(1) AS countPerson
FROM exam A
JOIN exam_user B
ON A.examId=B.examId
GROUP BY A.examName
HAVING COUNT(1)>=2

---结果---
examName countPerson
-------- -----------
j2EE 2
j2ME 3
j2SE 3

(所影响的行数为 3 行)
nianran520 2010-02-04
  • 打赏
  • 举报
回复
--写少了
select t.examName,isnull(r.countPerson,0) as countPerson
from exam t left join
(select examId,count(1) as countPerson from exam_user group by examId) r
on t.examId=r.examId
where isnull(r.countPerson,0) >= 2
bancxc 2010-02-04
  • 打赏
  • 举报
回复
select exam.*,t.countperson
from exam
join
(
select examId,count(*) as countperson
from exam_user
group by examId
) t on exam.examid=t.examId and t.countperson>=2
nianran520 2010-02-04
  • 打赏
  • 举报
回复
select t.examName,isnull(r.countPerson,0) as countPerson
from exam t left join
(select examId,count(1) as countPerson from exam_user) r
on t.examId=r.examId
where isnull(r.countPerson,0) >= 2
百年树人 2010-02-04
  • 打赏
  • 举报
回复
TRY
SELECT A.examName,COUNT(1) AS countPerson
FROM exam A
JOIN exam_user B
ON A.examId=B.examId
HAVING COUNT(1)>=2
-狙击手- 2010-02-04
  • 打赏
  • 举报
回复
好长
chuifengde 2010-02-04
  • 打赏
  • 举报
回复
SELECT examName,count(1) from 
exam a INNER JOIN exam_user b
ON a.examid=b.examid
GROUP BY examName
HAVING count(1)>2
lxyd000 2010-02-04
  • 打赏
  • 举报
回复
哦 谢了。
jack15850798154 2010-02-04
  • 打赏
  • 举报
回复
SELECT A.examName,sum(A.examPrice)/count(1) asdfd, COUNT(1) AS countPerson
FROM exam A
JOIN exam_user B
ON A.examId=B.examId
GROUP BY A.examName
HAVING COUNT(1)>=2 改成这样就可以了。违反了GROUP
jack15850798154 2010-02-04
  • 打赏
  • 举报
回复
SELECT A.examName,sum(A.examPrice)/count(1) examprice, COUNT(1) AS countPerson
FROM exam A
JOIN exam_user B
ON A.examId=B.examId
GROUP BY A.examName
HAVING COUNT(1)>=2
这样就可以了,我个人认为是因为,“A.examPrice”不只一条数据吧和GROUP BY有冲突。不知道我这样理解对不?如果错误还请包涵啊。
jack15850798154 2010-02-04
  • 打赏
  • 举报
回复
因为如果数据中有null值的话count不计数,
count(1)就是为了避免null,计算包括null值的数
理论上说count(*)和count(1) 是一个效果,不应该有问题啊
加载更多回复(4)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧