0
我有一個頁面,顯示了領先榜分數已在系統中,存儲在我的數據庫中註冊的所有用戶添加新的價值。MySQL的更新值與舊值
表1:
Points_hisotry
表:
| points_id || user_id(fk) || point_hist |
|___________||_____________||____________|
| 1 || 10 || 100 |
|___________||_____________||____________|
| 2 || 11 || 30 |
|___________||_____________||____________|
| 3 || 11 || 70 |
|___________||_____________||____________|
| 4 || 11 || 200 |
表2:
Users
表:
| users_id || username || firstname || lastname ||
|__________||___________||____________||__________||
| 10 || alan1 || Alan || Smith ||
|__________||___________||____________||__________||
| 11 || Jaz12 || Jass || Hopal ||
|__________||___________||____________||__________||
| 12 || Shubs || shubs || hawash ||
|__________||___________||____________||__________||
| 13 || John || Rob || engli ||
在points_history
表,我有3行與同users_id
,我需要他們來概括,所以我最終與總Point_hist
爲users_id
應加起來300
我需要一個查詢來幫我加入這些表格,然後將相同的users_id
的行相加爲一,然後在記分板上打印出來。
我已經試過很多的查詢,但我還沒有得到它的權利。
下面是我的一些PHP從leaderboard.php:
$sql = "SELECT * FROM users, points_history WHERE users.users_id = points_history.users_id";
$user_query = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$username = $row ["username"];
$point_hist = $row["point_hist"];
上面的查詢是打印出所有username
的,從以上兩個表自己的觀點。
我是新來這個,所以我需要一些幫助,請。
上面的代碼工作,但它讓我對整個表給每個用戶的total_points。而不是每個用戶對每個用戶的total_points。 @kingkero – user3443701
我想我需要一個'WHERE'條件來聲明WHERE users.users_id ='$ id'',但是我會如何將它添加到上面的查詢中。 @kingkero – user3443701
@ user3443701 Mhm在我的[小提琴](http://sqlfiddle.com/#!2/17964a/6)它的工作,也許嘗試'GROUP BY points_history.users_id' – kero