2

enter image description here

I have two table user and follow. I want to write view such that it will fetch all details of particular user along with that two extra column as follower count and followee count alias.

eg. user id=11 then all details from user tables plus followcount 1 and followed count1

3
  • Why have you included all databases ? Include the one which you are using. Commented Feb 21, 2014 at 6:28
  • @agnes i am not including multiple db i want to fetch data from multiple tables Commented Feb 21, 2014 at 6:29
  • 1
    I am talking about your tags which you applied to your question Commented Feb 21, 2014 at 6:30

2 Answers 2

2
SELECT u.id, 
       u.userid, 
       u.name, 
       u.mobile, 
       (SELECT Count(*) 
        FROM   follow f 
        WHERE  f.followerid = u.userid) AS follower, 
       (SELECT Count(*) 
        FROM   follow f 
        WHERE  f.followeeid = u.userid) AS followee 
FROM   users u 
Sign up to request clarification or add additional context in comments.

Comments

0

You can achieve this is by using JOIN statements in your query:

example of how you can achieve your final result:

CREATE VIEW [Followers] AS 
SELECT a.name, a.email, a.mobile, COUNT(SELECT COUNT(followerID) FROM follow WHERE followerID = a.userid), COUNT(SELECT COUNT(followeeID) FROM follow WHERE followeeID = a.userid) FROM users a INNER JOIN follow b ON b.followerID = a.userid

1 Comment

above solution gives onlu users details but i need count of users follower and followee in same query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.