vendredi 1 juillet 2016

How to order sql query by other table values

i know that stackoverflow is remember answer for this question, but I have a bit different situation. I have a lot cells in second table, but SQL query get only first. Ok, not all query, only WHERE tag.

Ex:
1 table:

user_id = 1
user_id = 2

2 table:

user_id = 1 | year = 2015 | rating = 55
user_id = 1 | year = 2016 | rating = 10
user_id = 2 | year = 2016 | rating = 50
user_id = 2 | year = 2016 | rating = 5

SQL query:

$query = "SELECT c.*,v.upvotes
        FROM ".PREFIX."_users c
        LEFT JOIN (SELECT user_id,pol,vid_sporta,year_sport,category,SUM(rating) as upvotes
        FROM ".PREFIX."_userrating
        GROUP BY user_id
        ) v
        ON c.user_id = v.user_id
        WHERE year_sport='2015'
        ORDER BY upvotes DESC";

This query give me only one user, whitch has 2015 first.

I need some while cycle in SQL query :) Help, please :)

Aucun commentaire:

Enregistrer un commentaire