I'd like to count the first 3 users who has the most attributed lines.
SQL Table:
ID | IdUser | Type |
-----------------------
0 | 1 | like |
1 | 1 | like |
2 | 4 | dislike |
3 | 5 | dislike |
4 | 1 | like |
5 | 4 | like |
6 | 5 | like |
8 | 4 | like |
9 | 4 | like |
10 | 3 | like |
11 | 5 | like |
12 | 9 | like |
Result should be:
idUser[1] with 3 times "like" and 0 "dislike" (3-0 = 3 points)
idUser[4] with 3 times "like" and 1 "dislike" (3-1 = 2 points)
idUser[5] with 2 times "like" and 1 "dislikes (2-1 = 1 point )
So what I'm trying to do is getting idUser 1 (3 points), then idUser4 (2 points) and finally idUser 5 (1 point) with their points.
I've tried different ways but none have worked.
Here I've tried to create a two-dimensional array with all data and then get the highest values but I couldn't do the second part.
Table 'users' has all users of the website table 'points' has likes and dislikes recorded
$sqlUsers = "SELECT * FROM users";
$resultUsers = $conn->query($sqlUsers);
$recordsArray = array(); //create array
while($rowUsers = $resultUsers->fetch_assoc()) {
$idUser = $rowUsers['id'];
//COUNT LIKES OF THE USER
$sqlLikes = "SELECT COUNT(id) AS numberLikes FROM points WHERE idCibledUser='$idUser' AND type='like'";
$resultLikes = $conn->query($sqlLikes);
$rowLikes = $resultLikes->fetch_assoc();
//COUNT DISLIKES OF THE USER
$sqlDislikes = "SELECT COUNT(id) AS numberDislikes FROM points WHERE idCibledUser='$idUser' AND type='dislike'";
$resultDislikes = $conn->query($sqlDislikes);
$rowDislikes = $resultDislikes->fetch_assoc();
//GET POINTS BY SUBTRACTING DISLIKES FROM LIKES
$points = $rowLikes['numberLikes'] - $rowDislikes['numberDislikes'];
$recordsArray[] = array($idUser => $points);
}