GROUP BY lname ORDER BY showing wrong results

前端 未结 2 1805
猫巷女王i
猫巷女王i 2021-01-25 10:00

This query I\'ve written, listing the top 25 in a sales competition, is showing the wrong results.

Users aren\'t showing that should be pretty far up on the list. Any id

2条回答
  •  借酒劲吻你
    2021-01-25 10:18

    Try using a proper, ANSI standard GROUP BY

    SELECT
        u.fname, u.lname, SUM(p.point) as points
    FROM 
        comp_sale s
        JOIN
        comp_product p ON s.prod_id = p.product_id 
        JOIN
        comp_user u ON s.sale_id = u.wp_id
    GROUP BY
        u.fname, u.lname
    ORDER BY 
        points DESC 
    LIMIT 25
    

    Also, use explicit JOINs for clarity

提交回复
热议问题