I have a table named egsmaster mysql workbench version : MySQL Workbench was 8.0.27
so i have this columns named number_id, product, french, german, english, italian this language are the category for the product so i want to get a return list of distinct record from all the columns,
first get the distinct record from french, then german, english and italian so i did that
here is my query
SELECT
COALESCE(category_french, '') AS french,
COALESCE(category_german, '') AS german,
COALESCE(category_english, '') AS english,
COALESCE(category_italian, '') AS italian
FROM (
SELECT DISTINCT
category_french,
category_german,
category_english,
category_italian
FROM
egsmaster
) AS t
WHERE
COALESCE(category_french, '') <> ''
OR COALESCE(category_german, '') <> ''
OR COALESCE(category_english, '') <> ''
OR COALESCE(category_italian, '') <> ''
AND (category_french IS NOT NULL
OR category_german IS NOT NULL
OR category_english IS NOT NULL
OR category_italian IS NOT NULL);
if i run this query i will get a result set like this
french | german | english | italian
tree | tall | hall | itall
| aaa | |
| | fff |
| | | yyyy
| bds | bgs | itall
trek | tall | |
| | | tr
trq | sss | ggg | tret
12 | re | a12 | ass
arrow | irrow | |
back | | | bock
sap | | | Sip
| Itat | itaa |
| | Iasd | kaka
| | | sa,p
| | | lasut
but what i want is to have a clean result set so that if i export it in excel it will show like this
french | german | english | italian |
tree aaa fff yyyy
trek bds lasd tr
trq itat sa,p
12 lasut
arrow
back
sap
in a row if it has multiple columns that contains a data the first column where it occurs must be the only that needs to return
regardless with the id what i only need to do is get the distinct data from all of the language column
Here's an idea however, this only works on MySQL v8+.
I'll be using
Common Table Expression (cte)andROW_NUMBER()as main function and useIFNULL()as optional function - only if your data consists of bothNULLand''for empty values.First, we'll be using your example query as base and wrap that in the first
ctelike this:Now we use
cat_tblin our secondcteto generateROW_NUMBER():Now we have the row number as reference for the next row number that we'll generate separately for each language column. According to your data sample,
category_frenchis the first column to be list out. Here's actefor it:cat_fris thectename with condition wherecategory_frenchcolumn value is not empty (orNULL).Note: As I mentioned, if you're not sure empty data value is either
NULLor'', then you can useIFNULL()function like in the query. However, if empty values are alwaysNULLthen you can change the partIFNULL(column_name,'')!=''tocolumn_name IS NOT NULL. Or if empty column always''then simplycolumn_name != ''.The next
cteis forcategory_german:The difference between this
cat_grandcat_frcte are obviously the column inSELECTwill becategory_germanwith an additionalIFNULL(category_french,'')=''inWHEREto indicate thatcat_grshould only return value whencategory_frenchis empty. And the rest of thectewill have the sameWHEREwith additional checking of each previous column value that will end up something like this:Now that we have all the
cteprepared, we can simply useLEFT JOINto get the desired result:Result:
Get the full query in this demo fiddle