Result of order for same query appearing different in MySQL 5.x.x and 8.x.x versions

28 Views Asked by At

My staging environment has MySQL 5.x.x version and production has 8.x.x version. MySQL query is written in Codeigniter 4 MVC.

When i execute code containing MySQL query on Staging environment, the sequence of rows are appearing different than the query executed on production. Both environment have same DB data. I could not use ORDER BY clause because further i'm using GROUP BY in same query (JOIN) which should return me 1st appeared row. And because of different IDs found after GROUP BY, Staging and Production server behavior is appearing different.

Below is query -

SELECT `A`.`qam_id`, `B`.`care_giver_name`, `B`.`care_cli_id`, `E`.* 
FROM `tbl_client_qtly_assessment_master` AS `A` 
JOIN `tbl_care_note_review_form` AS `B` ON `A`.`qam_id` = `B`.`care_qam_id` 
JOIN `tbl_clients` AS `C` ON `A`.`qam_cli_id` = `C`.`cli_id` 
JOIN `tbl_client_emergency_contact` AS `D` ON `B`.`care_giver_name` = `D`.`emc_id` 
LEFT JOIN `tbl_generate_stipend_payment` AS `E` ON `E`.`stipend_qam_id` = `A`.`qam_id` 
WHERE `cli_dcc_id` = '69' 
 AND `qam_type` = 'Care Note' 
 AND `qam_status` = 'ACTIVE' 
 AND B.care_giver_name = 14385 
 AND( `care_date_sun` >= "2024-01-07" 
  AND `care_date_sat` <= "2024-01-20" ) 
 AND `A`.`qam_complete_status` != 'STRIKED' 
GROUP BY `B`.`care_giver_name`, `B`.`care_cli_id` 
ORDER BY CAST(cli_client_id AS UNSIGNED) ASC LIMIT 20;

In above query, table 'tbl_generate_stipend_payment' has LEFT JOIN and will return multiple rows. But at end of query we have GROUP BY clause, which will pick 1st row from tbl_generate_stipend_payment table.

Below are the order of result when i removed GROUP BY-

Result on 5.x.x - enter image description here

Result on 8.x.x - enter image description here

So, Staging & Production servers are picking 1st row after applying GROUP BY which are different IDs when compare. Result need to have in same order so that row after GROUP BY should return same data in Staging & Production. Please suggest if there any solution.

0

There are 0 best solutions below