Find duplicate entries and give result in concat line

I have 2 tables where some records will be identical (except id)

Table a:

id, numA, codeA 6, 34, aa 7, 34, bb 8, 567, bc 

Table B

 id, numB, codeB 1, 34, aa 2, 34, bb 3, 567, bc 

I need to run a query in table B that will check if a given combination of number and code exists in table A and give the result in this format:

 num, concat code 34, (aa,bb) 567, (bc) 
+4
source share
1 answer

Join two tables and use GROUP_CONCAT

 SELECT a.NumA, GROUP_CONCAT(DISTINCT b.codeB) FROM table1 A INNER JOIN table2 b on a.numA = b.numB GROUP BY a.NumA 

SQLFiddle Demo

+4
source

Source: https://habr.com/ru/post/1440588/


All Articles