Click here to Skip to main content
15,906,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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


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

Regards,
Ismail
Posted
Updated 14-Apr-11 18:46pm
v3

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

Another thing, don't use nolock unless you really have to (for good reasons).
 
Share this answer
 
Try this baby boy.

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


Good luck!
 
Share this answer
 
Comments
Pong D. Panda 17-Apr-11 21:53pm    
Whoever downvoted my answer, can you further explain why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900