Jump to content

Why is this not ordering correctly? SQL


Mutley

Recommended Posts

SELECT t.team_id, t.level, t.name, t.initials, t.owner, l.user_id, SUM(l.2playerpts) AS 2playerpts, SUM(l.4playerpts) AS 4playerpts,SUM(l.6playerpts) AS 6playerpts, SUM(l.8playerpts) AS 8playerpts, l.wins, l.losses, u.user_id, u.team_id
		FROM teams t
		INNER JOIN users u ON u.team_id = t.team_id
		INNER JOIN leaderboard l ON l.user_id = u.user_id
		GROUP BY t.team_id
		ORDER BY (2playerpts + 4playerpts + 6playerpts + 8playerpts) DESC

 

What's happening is it's not ordering in any order, it should have the calculated row with the biggest Total SUM at the top.

 

I hope that makes sense. Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/122012-why-is-this-not-ordering-correctly-sql/
Share on other sites

SELECT t.team_id, t.level, t.name, t.initials, t.owner, l.user_id, SUM(l.2playerpts) AS 2playerpts, SUM(l.4playerpts) AS 4playerpts,SUM(l.6playerpts) AS 6playerpts, SUM(l.8playerpts) AS 8playerpts, l.wins, l.losses, u.user_id, u.team_id
		FROM teams t
		INNER JOIN users u ON u.team_id = t.team_id
		INNER JOIN leaderboard l ON l.user_id = u.user_id
		GROUP BY t.team_id
		ORDER BY (2playerpts + 4playerpts + 6playerpts + 8playerpts) DESC

What is t.team, l.wins and u.user_id? What are all the t, l and u?

If I change the alias for the SUMs it doesn't recognize them in the ORDER BY (it thinks their a field in the actual table).

 

SELECT t.team_id, t.level, t.name, t.initials, t.owner, l.user_id, SUM(l.2playerpts) AS vr2playerpts, SUM(l.4playerpts) AS vr4playerpts,SUM(l.6playerpts) AS vr6playerpts, SUM(l.8playerpts) AS vr8playerpts, l.wins, l.losses, u.user_id, u.team_id
		FROM teams t
		INNER JOIN users u ON u.team_id = t.team_id
		INNER JOIN leaderboard l ON l.user_id = u.user_id
		GROUP BY t.team_id
		ORDER BY (2playerpts + 4playerpts + 6playerpts + 8playerpts) DESC

 

So I did it like that^ but still no luck, it's like the 2playerpts is being grabbed from somewhere else but why doesn't my SUM aliases work in there?

try

SELECT t.team_id, t.level, t.name, t.initials, t.owner, l.user_id, SUM(l.2playerpts) AS vr2playerpts, SUM(l.4playerpts) AS vr4playerpts,SUM(l.6playerpts) AS vr6playerpts, SUM(l.8playerpts) AS vr8playerpts, l.wins, l.losses, u.user_id, u.team_id
		FROM teams t
		INNER JOIN users u ON u.team_id = t.team_id
		INNER JOIN leaderboard l ON l.user_id = u.user_id
		GROUP BY t.team_id
		ORDER BY SUM(2playerpts + 4playerpts + 6playerpts + 8playerpts) DESC

 

try this, it is somewhat of a guess,but intval may helpinthe sorting

 

 

SELECT t.team_id, t.level, t.name, t.initials, t.owner, l.user_id, SUM(l.2playerpts) AS 2playerpts, SUM(l.4playerpts) AS 4playerpts,SUM(l.6playerpts) AS 6playerpts, SUM(l.8playerpts) AS 8playerpts, l.wins, l.losses, u.user_id, u.team_id
		FROM teams t
		INNER JOIN users u ON u.team_id = t.team_id
		INNER JOIN leaderboard l ON l.user_id = u.user_id
		GROUP BY t.team_id
$sum_on_this = intval((2playerpts + 4playerpts + 6playerpts + 8playerpts));
		ORDER BY $sum_on_this  DESC	

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.