Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL:Getting count from many tables for a user record in USER table.Whats the best approach?
    primarykey
    data
    text
    <p>I have 4 SQL server(2008 version) tables </p> <pre><code>1) USER- to store user information (Fields : UserId,UserName) 2) FILES - to store files uploaded by user (FileId,FileName,UserId) 3) PHOTOS -to store files uploaded by user (PhotoId,PhotoName,UserId) 4) GROUPS= to store groups created by user ( GroupId,GroupName,UserId) </code></pre> <p>Now I want to get a USER record with id=5 along with Total number of Files uploaded by userid 5,Total Photos uploaded by user id 5,Total groups created by userid 5.Can i get all these with a single query ? OR should i write a SELECT COUNT of each table with USER ID=5 in the where clause ( 3 queries.. ?)</p> <p>I can write an inner join with USER and FILES and do the count to get the number of files uploaded by a purticular user.But how to add the number of photos and number of groups to the result ?</p> <p>What is the best approach to do this ?</p> <p>Sample Data</p> <h2>USER</h2> <pre><code>USERID USERNAME ------ --------- 1 Shyju 2 OMG 3 Gus </code></pre> <h2>FILES</h2> <pre><code>FILEID FILENAME USERID ------ -------- ------ 101 Sample1 1 102 Secondone 1 103 NewOne 2 104 BetterOne 3 105 Ocean 3 106 Sea 3 </code></pre> <h2>GROUPS</h2> <pre><code>GROUPID GROUPNAME USERID ------- --------- ------ 51 Group-A 1 52 Group-B 2 53 Group-C 2 54 Group-D 2 55 Group-E 3 56 Group-F 3 </code></pre> <p>The Result i am looking for is</p> <h2>For User ID=1</h2> <pre><code> USERID USERNAME FILECOUNT GROUPCOUNT ------ -------- --------- ---------- 1 Shyju 2 1 </code></pre> <h2>For UserId=2</h2> <pre><code> USERID USERNAME FILECOUNT GROUPCOUNT ------ -------- --------- ---------- 2 OMG 1 3 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload