SaranacLake Posted February 12, 2021 Share Posted February 12, 2021 This question may sound like a MySQL question, but I think it relates more to PHP... Currently I have the following (pseudo-code) query in my PHP script... SELECT id, col-a, col-b, col-c, usefulCount, usefulResponses, (usefulCount/usefulResposnes) as usefulPct newMedianCalculationHere FROM( SELECT DISTINCT col-a, col-b, col-c, (SELECT COUNT(xtc.id) FROM table-7 INNER JOIN table-8 ON WHERE something AND respone_yn = 1) AS usefulCount, (SELECT COUNT(xtc.id) FROM table-7 INNER JOIN table-8 ON something WHERE something) AS usefulResponses (Add Median calculation here) AS newMedianCalculationHere FROM INNER JOIN WHERE ) AS wrapper ORDER BY whatever Now I need to calculate the median of some data and drop that in my inner query. Here is some sample code that I found online... https://www.eversql.com/how-to-calculate-median-value-in-mysql-using-a-simple-sql-query/ SET @rowindex := -1; SELECT AVG(g.grade) FROM (SELECT @rowindex:=@rowindex + 1 AS rowindex, grades.grade AS grade FROM grades ORDER BY grades.grade) AS g WHERE g.rowindex IN (FLOOR(@rowindex / 2) , CEIL(@rowindex / 2)); Because my query is in a PHP script, I'm now sure how to incorporate these lines... SET @rowindex := -1; (SELECT @rowindex:=@rowindex + 1 AS rowindex, Oh, if it matters, I use prepared statements in my PHP... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/312132-calculate-median-in-query-in-my-php/ 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.