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
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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

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	

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.