Jump to content

Complicated (for me) Order By issue...


Jim R

Recommended Posts

I want to get the most recent 15 instances of my a_players_review table THEN sort them by grade, then last name.  Ordering the r.id first just gives me a list of rows that I can't do much else with, unless I'm missing how I can code the output. 

Below is what I had.  ORDER BY looked good until I started filling in more rows and seeing kids in grade = 2021 pushing more recent rows off the list.  

Nested ORDER BY seems frowned upon, and I'm not even sure they work.  I kept getting parameters errors.  I really can't make heads or tails of nested SELECTs.  That might be the answer, but I've been unable to take what I've seen and apply it without getting a parameters error.  

 

 

		$query = "SELECT *,p.id,b.playerID,b.id,s.toggle AS stoggle,o.toggle AS otoggle,p.city,p.school,s.city,s.school,r.opp_city,r.opp_school,o.city,o.school,r.city,r.school 
		FROM a_players_reviews r 
		
		LEFT JOIN a_players p 
			ON CONCAT (r.nameFirst,r.nameLast) = CONCAT (p.nameFirst,p.nameLast)
	
		LEFT JOIN a_schools s 
			ON CONCAT(r.city,r.school) = CONCAT(s.city,s.school)
		LEFT JOIN a_schools o	
		 	ON CONCAT(r.opp_city,r.opp_school) = CONCAT(o.city,o.school)
		
		LEFT JOIN a_player_bookmark b
			ON p.id = b.playerID && '". $userID ."' = b.userID
		
		WHERE p.id = b.playerID && '". $userID ."' = b.userID && bookmark>0
		ORDER BY p.grade,p.nameLast,r.id desc
		LIMIT 15
		";

 

Edited by Jim R
Link to comment
Share on other sites

How do you determine the most recent entries from a_players_reviews?  Is there a datetime column to sort by? Do you want just the most recent row for each player, or the most recent 15 even if multiple of them are from the same player?

Sample data and desired output would go a long way to describing what you want.

Link to comment
Share on other sites

14 hours ago, kicken said:

How do you determine the most recent entries from a_players_reviews?  Is there a datetime column to sort by? Do you want just the most recent row for each player, or the most recent 15 even if multiple of them are from the same player?

Sample data and desired output would go a long way to describing what you want.

Just the most recent 15 rows--so a_players_reviews 'r.id'.  I have a timestamp as well, but r.id is the same.  

From there, I want to organize them by grade then last name.  

I did try r.id first in the Order By, but unless I'm missing something, that's not going to allow me to organize them the way I want to.  I tried Group By r.id as well with Order By p.grade,p.nameLast, but same result (which makes sense).  

 

 

Link to comment
Share on other sites

You'll want to do a sub-query to get the most recent 15 in order by the ID, then sort the results of that sub query how you want.

SELECT *
FROM (
    SELECT 
        p.id
        , b.playerID
        , b.id
        , s.toggle AS stoggle
        , o.toggle AS otoggle
        , p.city
        , p.school
        , s.city
        , s.school
        , r.opp_city
        , r.opp_school
        , o.city
        , o.school
        , r.city
        , r.school
        , p.grade
        , p.nameLast
    FROM a_players_reviews r 
    INNER JOIN a_players p ON CONCAT (r.nameFirst,r.nameLast) = CONCAT (p.nameFirst,p.nameLast)
    INNER JOIN a_player_bookmark b ON p.id = b.playerID AND '". $userID ."' = b.userID
    LEFT JOIN a_schools s ON CONCAT(r.city,r.school) = CONCAT(s.city,s.school)
    LEFT JOIN a_schools o ON CONCAT(r.opp_city,r.opp_school) = CONCAT(o.city,o.school)
    WHERE 
       b.bookmark > 0
    ORDER BY r.id desc
    LIMIT 15
) mostRecent
ORDER BY
    mostRecent.grade
    , mostRecent.nameLast


Some additional notes, if you're going to create a WHERE condition using columns from a joined table, you probably want to use INNER JOIN rather than LEFT JOIN unless you're accounting for NULL values.   In general you should use INNER JOIN always unless you have specific reason to use a LEFT JOIN.

Your p.id = b.playerID and '$userID' = b.userID conditions are duplicated between your join and WHERE clauses, you only need them in one place, and that is the ON clause so remove them from the WHERE.

You didn't qualify your bookmark column with a table but I'm assuming it probably belongs to the a_player_bookmark table (if that's wrong this advice may not apply).  If that's the case then your b.bookmark > 0 condition effectively means you're INNER JOINing that table since any null values would fail.  As such you can just convert that join to INNER JOIN.  If you're intentionally looking for players without a bookmark entry (where the join fails) then you need to make that condition part of the ON clause and use a LEFT JOIN.

 

Edited by kicken
Link to comment
Share on other sites

I'm getting a syntax error:

 

Quote

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM a_players_reviews r INNER JOIN a_players p ON CONCAT (r.nameFirst' 

 

Link to comment
Share on other sites

I've been searching for other samples on the Googles and play around with pid / p.id, I removed it from the SELECT, and the error moved to the next p. instance, 'p.city', saying it was a duplicate column name.  Removed all the p. columns, and it turned to having a duplicate city column.  

I removed all the column names in the sub select, leaving just * , and I was back to having duplicate column 'id'.  

 

Is it not seeing any of the aliases?  

Link to comment
Share on other sites

10 hours ago, Jim R said:

Unknown column 'pid' in 'on clause'

You only use the aliases in the SELECT clause, not your joins or where clauses.  Continue to use p.id in those areas, but alias it in the select using p.id as pid

Since it's a sub-query each column in the SELECT has to have a unique name so alias any duplicates to something unique.  Then in your outer query you use the aliases instead of the original name.

Edited by kicken
Link to comment
Share on other sites

2 hours ago, kicken said:

You only use the aliases in the SELECT clause, not your joins or where clauses.  Continue to use p.id in those areas, but alias it in the select using p.id as pid

Since it's a sub-query each column in the SELECT has to have a unique name so alias any duplicates to something unique.  Then in your outer query you use the aliases instead of the original name.

But that gets me back to the duplicate column error.  I only messed around with the aliases because of that.  

 

So what you modified sent the duplicate column error.  

Link to comment
Share on other sites

Getting this error:

Unknown column 'rfirst' in 'on clause'

 

$query = "SELECT * FROM (SELECT *,p.id as pid,
		b.playerID as bplayerID,
		b.userID as buserID,
		s.toggle AS stoggle,
		o.toggle AS otoggle,
		p.city as pcity,
		p.school as pschool,
		s.city as scity,
		s.school as sschool,
		r.id as rid,
		r.opp_city as roppcity,
		r.opp_school as roppschool,
		o.city as ocity,
		o.school as oschool,
		r.city as rcity,
		r.school as rschool,
		r.nameFirst as rfirst,
		r.nameLast as rlast,
		p.nameFirst as pfirst,
		p.nameLast as plast
		
		FROM a_players_reviews r 
		
		LEFT JOIN a_players p 
			ON CONCAT (rfirst,rlast) = CONCAT (pfirst,plast)
	
		LEFT JOIN a_schools s 
			ON CONCAT(rcity,rschool) = CONCAT(scity,sschool)
		LEFT JOIN a_schools o	
		 	ON CONCAT(roppcity,roppschool) = CONCAT(ocity,oschool)
		
		LEFT JOIN a_player_bookmark b
			ON pid = bplayerID && '". $userID ."' = buserID
		
		WHERE bookmark>0
		ORDER BY rid desc
		LIMIT 15) mostRecent
 			ORDER BY mostRecent.grade,mostRecent.nameLast	
		";

 

Link to comment
Share on other sites

36 minutes ago, Jim R said:

Getting this error:

Unknown column 'rfirst' in 'on clause'

And I told you:

5 hours ago, kicken said:

You only use the aliases in the SELECT clause, not your joins or where clauses.  Continue to use p.id in those areas, but alias it in the select using p.id as pid

The aliases are only for your SELECT and the outer query.  Continue using the table.column name syntax in your conditions and joins.

 

Link to comment
Share on other sites

Now I'm getting...

 

Duplicate column name 'id'
 

$query = "SELECT * FROM (SELECT *,p.id as pid,
		b.playerID as bplayerID,
		b.userID as buserID,
		s.toggle AS stoggle,
		o.toggle AS otoggle,
		p.city as pcity,
		p.school as pschool,
		s.city as scity,
		s.school as sschool,
		r.id as rid,
		r.opp_city as roppcity,
		r.opp_school as roppschool,
		o.city as ocity,
		o.school as oschool,
		r.city as rcity,
		r.school as rschool,
		r.nameFirst as rfirst,
		r.nameLast as rlast,
		p.nameFirst as pfirst,
		p.nameLast as plast
		
		FROM a_players_reviews r 
		
		LEFT JOIN a_players p 
			ON CONCAT (r.nameFirst,r.nameLast) = CONCAT (p.nameFirst,p.nameLast)
	
		LEFT JOIN a_schools s 
			ON CONCAT(r.city,r.school) = CONCAT(s.city,s.school)
		LEFT JOIN a_schools o	
		 	ON CONCAT(r.opp_city,r.opp_school) = CONCAT(o.city,o.school)
		
		LEFT JOIN a_player_bookmark b
			ON p.id = b.playerID && '". $userID ."' = b.userID
		
		WHERE bookmark>0
		ORDER BY r.id desc
		LIMIT 15) mostRecent
 			ORDER BY mostRecent.grade,mostRecent.nameLast	
		";

 

Link to comment
Share on other sites

You need have whatever columns you want to order by in the outer query as part of the select clause of the inner query.  If you check my original query you'll see I added the grade and nameLast columns to the select list.

If you alias them, then use the alias in the order by clause of the outer query.

Link to comment
Share on other sites

6 minutes ago, kicken said:

You need have whatever columns you want to order by in the outer query as part of the select clause of the inner query.  If you check my original query you'll see I added the grade and nameLast columns to the select list.

If you alias them, then use the alias in the order by clause of the outer query.

 

I pasted your original query.  It yielded the same set of errors we're getting now, and the order by columns at the end were not in the original select list, certainly not with mostrecent.grade and mostrecent.nameLast.  Giving it a try.

Edited by Jim R
Link to comment
Share on other sites

I tried mostRecent.grade and mostRecent.nameLast in the inner select and outer select.  I tried them in both at the same time.  All three instances I got the following error:

 

Unknown column 'mostRecent.grade' in 'field list'

 

$query = "SELECT *,mostRecent.grade,
		mostRecent.nameLast
		
		FROM (SELECT p.id as pid,
		b.playerID as bplayerID,
		b.userID as buserID,
		s.toggle AS stoggle,
		o.toggle AS otoggle,
		p.city as pcity,
		p.school as pschool,
		s.city as scity,
		s.school as sschool,
		r.id as rid,
		r.opp_city as roppcity,
		r.opp_school as roppschool,
		o.city as ocity,
		o.school as oschool,
		r.city as rcity,
		r.school as rschool,
		r.nameFirst as rfirst,
		r.nameLast as rlast,
		p.nameFirst as pfirst,
		p.nameLast as plast
		
		
		FROM a_players_reviews r 
		
		LEFT JOIN a_players p 
			ON CONCAT (r.nameFirst,r.nameLast) = CONCAT (p.nameFirst,p.nameLast)
	
		LEFT JOIN a_schools s 
			ON CONCAT(r.city,r.school) = CONCAT(s.city,s.school)
		LEFT JOIN a_schools o	
		 	ON CONCAT(r.opp_city,r.opp_school) = CONCAT(o.city,o.school)
		
		LEFT JOIN a_player_bookmark b
			ON p.id = b.playerID && '". $userID ."' = b.userID
		
		WHERE bookmark>0
		ORDER BY r.id desc
		LIMIT 15) mostRecent
 			ORDER BY mostRecent.grade,mostRecent.nameLast	
		";

 

Edited by Jim R
Link to comment
Share on other sites

CRiqo6l.png

 

You need to select the columns in your inner query.  Alias them if needed.

Then in the outer query is where you use them.  mostRecent is the name assigned to the results of the inner query so it is only valid in the outer query and you use it to reference whichever columns you select.

 

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.