Skip to content
Advertisement

MySQL Trigger Nested Group_Concat Duplicates

I have a trigger to conditionally GROUP_CONCAT a few columns. I can remove duplicates from within a single column, but I’m struggling to remove duplicates between two or more columns.

Current trigger (two column example):

Fiddle: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=46a6bb915fa7e2e9106b073677eee6cd

Advertisement

Answer

Change the second Select to

✓

✓

id | selection1 | selection2 | results
-: | :--------- | :--------- | :------
 1 | null       | null       | null   
 2 | null       | null       | null   
 3 | null       | null       | null   
✓

✓

stocks1 | stocks2
:------ | :------
BRK     | GOOG   
INTC    | NKE    
TSLA    | APPL   
APPL    | NKE    
TSLA    | FB     
NKE     | BRK    
id | selection1 | selection2 | results                                             
-: | :--------- | :--------- | :---------------------------------------------------
 1 | x          | x          | <br>BRK<br>INTC<br>TSLA<br>APPL<br>NKE<br>GOOG<br>FB
 2 | null       | null       | null                                                
 3 | null       | null       | null                                                

db<>fiddle here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement