确定表中的计数 [英] To determine the count in the table

查看:57
本文介绍了确定表中的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我需要从两个表的连接中获取单个表的计数.
该查询在下面提到.

Hi all,
I need to get the count of single table from the join of two tables.
The query is mentioned below.

SELECT COUNT(''*'') FROM student_main  main	(Nolock), address arc (nolock)
WHERE  arc.id		= 20
and    main.stu_no 	= arc.stu_no



从上面的查询中,我需要对student_main表的计数.但是它得到了两个表的数量.
请告知...

问候,
Ismail



From the above query, i need the count of student_main table. But it gets the count of both tables.
Kindly advise...

Regards,
Ismail

推荐答案

如果联接两个表并使用count,则将获得结果集的计数,而不是表的计数.在您的示例中,您将获得地址为20的学生总数.

因此,如果要单独获取总数,则可以使用标量查询,例如:
If you join two tables and use count, you''ll have the count of the result set, not the table. In your example you would get the total amount of students whose address have id 20.

So if you want to get the total count separately, you can use for example scalar query for that, like:
SELECT main.*,
       arc.*,
       (SELECT COUNT(*) FROM student_main) AS TotalStudentAmount
FROM student_main  main, 
     address arc
WHERE  arc.id       = 20
AND    main.stu_no  = arc.stu_no


另一件事,除非确实出于充分原因,否则不要使用nolock(出于充分的理由).


Another thing, don''t use nolock unless you really have to (for good reasons).


尝试这个男婴.

Try this baby boy.

SELECT COUNT(*) FROM student_main  main   
WHERE main.stu_no in 
   (SELECT stu_no FROM address WHERE id = 20)



祝你好运!



Good luck!


这篇关于确定表中的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆