MySQL compares the same values ​​in two columns

I have data in my database:

jamu_a | jamu_b | khasiat

A      | B      | Z
A      | B      | X
A      | B      | C

And then, I want the result to be as follows:

jamu_a | jamu_b | khasiat | total

A      | B      | Z, X, C | 3

I am not an expert in MySQL, what is the request for creating such output? Tell me if MySQL cannot do this and need some kind of programming language. thanks in advance

+5
source share
1 answer
SELECT  jamu_a,
        jamu_b,
        GROUP_CONCAT(khasiat) khasiat,
        COUNT(*) total
FROM    TableName
GROUP   BY  jamu_a, jamu_b

OUTPUT

╔════════╦════════╦═════════╦═══════╗
β•‘ JAMU_A β•‘ JAMU_B β•‘ KHASIAT β•‘ TOTAL β•‘
╠════════╬════════╬═════════╬═══════╣
β•‘ A      β•‘ B      β•‘ Z,X,C   β•‘     3 β•‘
β•šβ•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•

if the column KHASIAThas duplicate values ​​and you want it to be unique, you can add DISTINCTtoGROUP_CONCAT()

SELECT  jamu_a,
        jamu_b,
        GROUP_CONCAT(DISTINCT khasiat) khasiat,
        COUNT(*) total
FROM    TableName
GROUP   BY  jamu_a, jamu_b
+10
source

All Articles