Jim R Posted April 7, 2020 Share Posted April 7, 2020 (edited) 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 April 7, 2020 by Jim R Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/ Share on other sites More sharing options...
Barand Posted April 7, 2020 Share Posted April 7, 2020 7 minutes ago, Jim R said: I want to get the most recent 15 instances of my a_players_review table You'll stand a better chance of doing that if it is one of the tables in the query. Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576535 Share on other sites More sharing options...
Jim R Posted April 7, 2020 Author Share Posted April 7, 2020 (edited) It is. Scroll across. (Either you didn't see it, or you're commenting on a typo in my explanation.) EDIT: Added a carriage return for visual assistance. Edited April 7, 2020 by Jim R Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576536 Share on other sites More sharing options...
Jim R Posted April 8, 2020 Author Share Posted April 8, 2020 Not sure what you were referring to. Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576565 Share on other sites More sharing options...
Barand Posted April 8, 2020 Share Posted April 8, 2020 I just didn't see the table - the end of that first line was somewhere in my neighbour's living room. 2 Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576571 Share on other sites More sharing options...
Jim R Posted April 13, 2020 Author Share Posted April 13, 2020 Any thoughts on how to get the results? Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576758 Share on other sites More sharing options...
kicken Posted April 13, 2020 Share Posted April 13, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576760 Share on other sites More sharing options...
Jim R Posted April 13, 2020 Author Share Posted April 13, 2020 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). Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576789 Share on other sites More sharing options...
kicken Posted April 14, 2020 Share Posted April 14, 2020 (edited) 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 April 14, 2020 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576796 Share on other sites More sharing options...
Jim R Posted April 14, 2020 Author Share Posted April 14, 2020 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' Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576802 Share on other sites More sharing options...
Jim R Posted April 14, 2020 Author Share Posted April 14, 2020 (edited) Disregard. I had an omission, but now I'm getting this error: Quote Duplicate column name 'id' Edited April 14, 2020 by Jim R Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576803 Share on other sites More sharing options...
Jim R Posted April 14, 2020 Author Share Posted April 14, 2020 (edited) I set up the p.id as pid, r.id as rid and b.id as bid and now it says: Quote Unknown column 'pid' in 'on clause' Edited April 14, 2020 by Jim R Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576804 Share on other sites More sharing options...
Jim R Posted April 14, 2020 Author Share Posted April 14, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576818 Share on other sites More sharing options...
kicken Posted April 14, 2020 Share Posted April 14, 2020 (edited) 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 April 14, 2020 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576820 Share on other sites More sharing options...
Jim R Posted April 14, 2020 Author Share Posted April 14, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576828 Share on other sites More sharing options...
kicken Posted April 14, 2020 Share Posted April 14, 2020 The code I posted needs to have aliases added to it as you discovered. If after adding those you're still having issues then you need to post the code you are using after you've applied your modifications. Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576842 Share on other sites More sharing options...
Jim R Posted April 14, 2020 Author Share Posted April 14, 2020 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 "; Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576846 Share on other sites More sharing options...
kicken Posted April 14, 2020 Share Posted April 14, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576850 Share on other sites More sharing options...
Jim R Posted April 14, 2020 Author Share Posted April 14, 2020 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 "; Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576852 Share on other sites More sharing options...
kicken Posted April 14, 2020 Share Posted April 14, 2020 Get rid of the * in your select. Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576853 Share on other sites More sharing options...
Jim R Posted April 14, 2020 Author Share Posted April 14, 2020 (edited) Quote Unknown column 'mostRecent.grade' in 'order clause' At least we're working down the query. 🥴 Edited April 14, 2020 by Jim R Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576854 Share on other sites More sharing options...
kicken Posted April 14, 2020 Share Posted April 14, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576855 Share on other sites More sharing options...
Jim R Posted April 14, 2020 Author Share Posted April 14, 2020 (edited) 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 April 14, 2020 by Jim R Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576857 Share on other sites More sharing options...
Jim R Posted April 14, 2020 Author Share Posted April 14, 2020 (edited) 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 April 14, 2020 by Jim R Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576858 Share on other sites More sharing options...
kicken Posted April 15, 2020 Share Posted April 15, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310497-complicated-for-me-order-by-issue/#findComment-1576862 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.