How to fetch multiple results that reside in one column

I am looking for a query that will return a count of the views column based on a single user's id.

Here is the table's contents:

id |  post_id | user_id | views

1 |      4     |   67    | 4
1 |      56     |   67    | 3
1 |      34     |   67    | 10

I want to get the total of posts views the user have

SELECT *,count(views) from users WHERE user_id = 67

The result have to be 17

Answers


SELECT user_id, sum(views) FROM users WHERE user_id = 67 GROUP BY user_id

You want the SUM operator.

SELECT SUM(views) FROM users WHERE user_id = 67

Just SUM up the views:

Simple approach:

SELECT SUM(views) 
FROM users
WHERE user_id = 67;

More complex approach:

SELECT user_id, SUM(views) 
FROM users
WHERE user_id = 67
GROUP BY user_id;

Need Your Help

Update SQL query

mysql sql sql-server

I am writting a SQL query , where I need to update the colouns in dummy coloumn with the value on the basis of same post_id such that for post_id '1' all the rows will have 'abc' in there dummy c...