2017-09-05 75 views
-2

居城市的結果我想基於MySQL的城市排名結果我想基於MySQL的

表1:-users

| user_id | marks | 
-------------------- 
| 1  | 10 | 
| 5  | 10 | 
| 5  | 50 | 
| 3  | 15 | 
| 4  | 10 | 

| 2  | 10 | 
| 6  | 10 | 
| 6  | 50 | 
| 4  | 15 | 
| 4  | 10 | 

表:-2用戶的詳細信息

| user_id | city   | 
-------------------- 
| 1  | newdelhi | 
| 2  | kolkata | 
| 3  | mumbai  | 
| 4  | newdelhi | 
| 5 

| 6  | newdelhi | 

我想結果是這樣的:

| user_id | points | 
-------------------- 
| 6  | 60 | 
| 4  | 35 | 
| 1  | 10 | 
+0

你嘗試過什麼到目前爲止,來解決這個問題? –

回答

0

試試這個:

SELECT 
    users.user_id 
    ,SUM(users.marks) AS points 
FROM 
    users 
INNER JOIN 
    users_details ON users.user_id = users_details.user_id 
WHERE 
    users_details.city = 'newdelhi' 
GROUP BY 
    user_id