Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate row after left join

I am trying to write a query which as follow:

select distinct  bsg.id as bsgId,
s.system_id as sysId,
g.code_no as gameNo,
u.user_name as nameOfUser,
s.score_code as scoreId,
p.name as cityOfGame

from score s
join scoreGr sg on sg.id = s.scoreGr_id
join bigScoreGr bsg on sg.bigScoreGr_id = bsg.id
join game g on bsg.fld_case_id = g.id
join user u on s.user_id = u.id
join system_number sn on g.id = sn.game_id
join system_doc sd on sd.system_number_id = sn.id
left join parameter p on sd.city_id = p.id

Until I have joined with parameter table, result is as expected. The result seems like below:

bsgId| sysId | gameNo | nameOfUser | scoreId
--------------------------------------------------
1234 | abcde | G-12   | admin      | G-12/1/1
1235 | abcdf | G-15   | admin      | G-15/1/3
1234 | abcdf | G-12   | user1      | G-12/1/8
1237 | abcdf | G-16   | user1      | G-16/2/4    

However, parameter table is something big and system_doc has some null values in its city_id column. When I add the left join part of my query, it becomes like that:

bsgId| sysId | gameNo | nameOfUser | scoreId  | city
--------------------------------------------------
1234 | abcde | G-12   | admin      | G-12/1/1 | city1
1235 | abcdf | G-15   | admin      | G-15/1/3 | city5
1235 | abcdf | G-15   | admin      | G-15/1/3 | 
1234 | abcdg | G-12   | user1      | G-12/1/8 | city4
1234 | abcdg | G-12   | user1      | G-12/1/8 | 
1237 | abcdf | G-16   | user1      | G-16/2/4 |

I do not want rows like 3rd and 5th ones. To avoid these rows which has null in their city columns and "has the exact same data except city field" (I mean city can be null actually,as in the last row, but having row #2 makes row #3 useless, so I only want row #2) I have used distinct on(scoreId), but it did not worked since I have lost row #2 but not row #3.

How could I eliminate those duplicate rows which has null in their city fields? I hope my question is clear.

like image 752
Sami Avatar asked May 19 '26 23:05

Sami


1 Answers

It's a postgresql bug.

left join parameter p on sd.city_id = p.id

Try this

left join parameter p on p.id = p.id
WHERE sd.city_id = p.id

(I have answered this so anyone looking for will now know about this bug)

like image 61
pbug Avatar answered May 22 '26 11:05

pbug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!