Код: Выделить всё
a_users
`id` int(10)
`display_name` varchar(250)
a_predictions
`user_id` int(10)
`match_id` int(10)
`home_score` tinyint(3)
`away_score` tinyint(3)
a_matches
`id` int(10)
`home_team_id` int(11)
`away_team_id` int(11)
`home_score` tinyint(3)
`away_score` tinyint(3)
`play_date` datetime
Data a_users:
id display_name
15 William
16 Albert
17 Wim
18 John
19 Henri
Data a_predictions
user_id match_id home_score away_score
16 149 2 1
16 150 1 3
16 160 1 3
16 161 2 0
16 171 4 0
16 172 2 1
16 182 2 0
16 183 2 1
16 192 0 3
16 193 3 1
16 203 2 1
16 204 2 1
18 149 1 1
18 171 2 1
18 182 1 1
18 203 2 1
19 150 1 2
19 161 3 1
19 172 1 1
19 183 1 2
19 193 2 0
19 204 3 1
Data a_matches
id home_team_id away_team_id home_score away_score play_date
149 23 107 [NULL] [NULL] 2025-09-27 12:30:00
160 36 28 [NULL] [NULL] 2025-09-27 12:30:00
203 73 75 [NULL] [NULL] 2025-09-28 12:00:00
192 78 47 [NULL] [NULL] 2025-09-28 12:00:00
182 94 64 [NULL] [NULL] 2025-09-28 12:00:00
171 13 2 [NULL] [NULL] 2025-09-28 12:00:00
161 28 103 [NULL] [NULL] 2025-10-04 12:30:00
150 88 23 [NULL] [NULL] 2025-10-04 12:30:00
172 2 82 [NULL] [NULL] 2025-10-05 12:00:00
183 110 64 [NULL] [NULL] 2025-10-05 12:00:00
193 47 98 [NULL] [NULL] 2025-10-05 12:00:00
204 97 73 [NULL] [NULL] 2025-10-05 12:00:00
< /code>
a_users- связан с a_matches с помощью a_predictions.id = a_matches.match_id
Код: Выделить всё
a_predictions
count of Matches < /th>
count of прогнозов < /th>
комментарии < /th>
< /tr>
< /thead>
< /td>
< /td>
< /td> /> 6 < /td>
6 < /td>
Все совпадения для этой недели выполнен < /td>
< /tr>
henri < /td>
6 < /td>
0 0
0
0
0
0
. a_predictions < /td>
< /tr>
Джон < /td>
6 < /td>
4 < /td>
2 для прогнозирования < /td>
< /tr>
/> 6 < /td>
0 < /td>
not intry not in a_predictions < /td>
< /tr>
< /tbody>
< /table> < /div>
Этот запрос показывает только названия пользователей, которые введены не менее 1 Predict:
.
Код: Выделить всё
SELECT
u.display_name Name,
COUNT(*) Predictions
FROM
a_users u
LEFT JOIN a_predictions p on u.ID = p.user_id
LEFT JOIN a_matches m on p.match_id = m.id
WHERE
m.play_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)
GROUP BY
u.id
ORDER BY
u.display_name
< /code>
Result:
Name
Predictions
Albert
6
John
4
Tried many other queries with left joinsПодробнее здесь: https://stackoverflow.com/questions/797 ... hird-table
Мобильная версия