Simplifying a MySQL Query with many left joins












1














I have a table which has tags which reference another table in multiple languages. I wrote this query which does the job but isn't elegant and its very long winded:



SELECT DISTINCT
T1.ID as CountryID,
T1.Type AS CountryType,
T2.Text AS CountryText,
T3.Text AS CountryTitle,
T4.Text AS Heading1,
T5.Text AS Heading2,
T6.Text AS Heading3,
T7.Text AS Heading4,
T8.Text AS Heading5,
T9.Text AS Heading6,
T10.Text AS Heading7,
T11.Text AS Heading8,
T12.Text AS Heading9,
T13.Text AS Heading10,
T14.Text AS Heading11,
T15.Text AS Heading12,
T16.Text AS Heading13,
T17.Text AS Heading14,
T18.Text AS Heading15,
T19.Text AS Heading16,
T20.Text AS Heading17,
T21.Text AS Heading18,
T22.Text AS Heading19,
T23.Text AS Heading20
FROM
Countrys AS T1
LEFT JOIN EN_TEXT AS T2 ON T1.DESCRIPTION=T2.TAG
LEFT JOIN EN_TEXT AS T3 ON CONCAT(T1.COUNTRYTAG,'_TITLE')=T3.TAG
LEFT JOIN EN_TEXT AS T4 ON CONCAT(T1.COUNTRYTAG,'_HEADING_1')=T4.TAG
LEFT JOIN EN_TEXT AS T5 ON CONCAT(T1.COUNTRYTAG,'_HEADING_2')=T5.TAG
LEFT JOIN EN_TEXT AS T6 ON CONCAT(T1.COUNTRYTAG,'_HEADING_3')=T6.TAG
LEFT JOIN EN_TEXT AS T7 ON CONCAT(T1.COUNTRYTAG,'_HEADING_4')=T7.TAG
LEFT JOIN EN_TEXT AS T8 ON CONCAT(T1.COUNTRYTAG,'_HEADING_5')=T8.TAG
LEFT JOIN EN_TEXT AS T9 ON CONCAT(T1.COUNTRYTAG,'_HEADING_6')=T9.TAG
LEFT JOIN EN_TEXT AS T10 ON CONCAT(T1.COUNTRYTAG,'_HEADING_7')=T10.TAG
LEFT JOIN EN_TEXT AS T11 ON CONCAT(T1.COUNTRYTAG,'_HEADING_8')=T11.TAG
LEFT JOIN EN_TEXT AS T12 ON CONCAT(T1.COUNTRYTAG,'_HEADING_9')=T12.TAG
LEFT JOIN EN_TEXT AS T13 ON CONCAT(T1.COUNTRYTAG,'_HEADING_10')=T13.TAG
LEFT JOIN EN_TEXT AS T14 ON CONCAT(T1.COUNTRYTAG,'_HEADING_11')=T14.TAG
LEFT JOIN EN_TEXT AS T15 ON CONCAT(T1.COUNTRYTAG,'_HEADING_12')=T15.TAG
LEFT JOIN EN_TEXT AS T16 ON CONCAT(T1.COUNTRYTAG,'_HEADING_13')=T16.TAG
LEFT JOIN EN_TEXT AS T17 ON CONCAT(T1.COUNTRYTAG,'_HEADING_14')=T17.TAG
LEFT JOIN EN_TEXT AS T18 ON CONCAT(T1.COUNTRYTAG,'_HEADING_15')=T18.TAG
LEFT JOIN EN_TEXT AS T19 ON CONCAT(T1.COUNTRYTAG,'_HEADING_16')=T19.TAG
LEFT JOIN EN_TEXT AS T20 ON CONCAT(T1.COUNTRYTAG,'_HEADING_17')=T20.TAG
LEFT JOIN EN_TEXT AS T21 ON CONCAT(T1.COUNTRYTAG,'_HEADING_18')=T21.TAG
LEFT JOIN EN_TEXT AS T22 ON CONCAT(T1.COUNTRYTAG,'_HEADING_19')=T22.TAG
LEFT JOIN EN_TEXT AS T23 ON CONCAT(T1.COUNTRYTAG,'_HEADING_20')=T23.TAG
WHERE
T1.Type='Country_ENGLAND';


Is there a way I can simplify this query so that I don't have to have all those LEFT JOINS? Different countries will have different number of headings so I have to get all possible headings to ensure I get them all.



Something like this - I know this wont work, I'm just giving an example of what I am trying to do.



SELECT DISTINCT
T1.ID as CountryID,
T1.Type AS CountryType,
T2.Text AS CountryText,
T3.Text AS CountryTitle,
T4.Text AS Headings?,

FROM
Countrys AS T1
LEFT JOIN EN_TEXT AS T2 ON T1.DESCRIPTION=T2.TAG
LEFT JOIN EN_TEXT AS T3 ON CONCAT(T1.COUNTRYTAG,'_TITLE')=T3.TAG
LEFT JOIN EN_TEXT AS T4 ON CONCAT(T1.COUNTRYTAG,'_HEADING_%')=T4.TAG AND T4.TAG IS NOT NULL,
WHERE
T1.Type='Country_ENGLAND';









share|improve this question
























  • . . This is what you get for storing data in columns that should actually be in rows. Your EN_TEXT table should have a separate row for each heading -- that is, by rows rather than by columns.
    – Gordon Linoff
    Nov 12 '18 at 12:10










  • Don't you have countrId (a FK) in the EN_TEXT table to connect to Countrys table ?
    – Madhur Bhaiya
    Nov 12 '18 at 12:10










  • What is the expected output? All en_text values as rows?
    – Salman A
    Nov 12 '18 at 12:21


















1














I have a table which has tags which reference another table in multiple languages. I wrote this query which does the job but isn't elegant and its very long winded:



SELECT DISTINCT
T1.ID as CountryID,
T1.Type AS CountryType,
T2.Text AS CountryText,
T3.Text AS CountryTitle,
T4.Text AS Heading1,
T5.Text AS Heading2,
T6.Text AS Heading3,
T7.Text AS Heading4,
T8.Text AS Heading5,
T9.Text AS Heading6,
T10.Text AS Heading7,
T11.Text AS Heading8,
T12.Text AS Heading9,
T13.Text AS Heading10,
T14.Text AS Heading11,
T15.Text AS Heading12,
T16.Text AS Heading13,
T17.Text AS Heading14,
T18.Text AS Heading15,
T19.Text AS Heading16,
T20.Text AS Heading17,
T21.Text AS Heading18,
T22.Text AS Heading19,
T23.Text AS Heading20
FROM
Countrys AS T1
LEFT JOIN EN_TEXT AS T2 ON T1.DESCRIPTION=T2.TAG
LEFT JOIN EN_TEXT AS T3 ON CONCAT(T1.COUNTRYTAG,'_TITLE')=T3.TAG
LEFT JOIN EN_TEXT AS T4 ON CONCAT(T1.COUNTRYTAG,'_HEADING_1')=T4.TAG
LEFT JOIN EN_TEXT AS T5 ON CONCAT(T1.COUNTRYTAG,'_HEADING_2')=T5.TAG
LEFT JOIN EN_TEXT AS T6 ON CONCAT(T1.COUNTRYTAG,'_HEADING_3')=T6.TAG
LEFT JOIN EN_TEXT AS T7 ON CONCAT(T1.COUNTRYTAG,'_HEADING_4')=T7.TAG
LEFT JOIN EN_TEXT AS T8 ON CONCAT(T1.COUNTRYTAG,'_HEADING_5')=T8.TAG
LEFT JOIN EN_TEXT AS T9 ON CONCAT(T1.COUNTRYTAG,'_HEADING_6')=T9.TAG
LEFT JOIN EN_TEXT AS T10 ON CONCAT(T1.COUNTRYTAG,'_HEADING_7')=T10.TAG
LEFT JOIN EN_TEXT AS T11 ON CONCAT(T1.COUNTRYTAG,'_HEADING_8')=T11.TAG
LEFT JOIN EN_TEXT AS T12 ON CONCAT(T1.COUNTRYTAG,'_HEADING_9')=T12.TAG
LEFT JOIN EN_TEXT AS T13 ON CONCAT(T1.COUNTRYTAG,'_HEADING_10')=T13.TAG
LEFT JOIN EN_TEXT AS T14 ON CONCAT(T1.COUNTRYTAG,'_HEADING_11')=T14.TAG
LEFT JOIN EN_TEXT AS T15 ON CONCAT(T1.COUNTRYTAG,'_HEADING_12')=T15.TAG
LEFT JOIN EN_TEXT AS T16 ON CONCAT(T1.COUNTRYTAG,'_HEADING_13')=T16.TAG
LEFT JOIN EN_TEXT AS T17 ON CONCAT(T1.COUNTRYTAG,'_HEADING_14')=T17.TAG
LEFT JOIN EN_TEXT AS T18 ON CONCAT(T1.COUNTRYTAG,'_HEADING_15')=T18.TAG
LEFT JOIN EN_TEXT AS T19 ON CONCAT(T1.COUNTRYTAG,'_HEADING_16')=T19.TAG
LEFT JOIN EN_TEXT AS T20 ON CONCAT(T1.COUNTRYTAG,'_HEADING_17')=T20.TAG
LEFT JOIN EN_TEXT AS T21 ON CONCAT(T1.COUNTRYTAG,'_HEADING_18')=T21.TAG
LEFT JOIN EN_TEXT AS T22 ON CONCAT(T1.COUNTRYTAG,'_HEADING_19')=T22.TAG
LEFT JOIN EN_TEXT AS T23 ON CONCAT(T1.COUNTRYTAG,'_HEADING_20')=T23.TAG
WHERE
T1.Type='Country_ENGLAND';


Is there a way I can simplify this query so that I don't have to have all those LEFT JOINS? Different countries will have different number of headings so I have to get all possible headings to ensure I get them all.



Something like this - I know this wont work, I'm just giving an example of what I am trying to do.



SELECT DISTINCT
T1.ID as CountryID,
T1.Type AS CountryType,
T2.Text AS CountryText,
T3.Text AS CountryTitle,
T4.Text AS Headings?,

FROM
Countrys AS T1
LEFT JOIN EN_TEXT AS T2 ON T1.DESCRIPTION=T2.TAG
LEFT JOIN EN_TEXT AS T3 ON CONCAT(T1.COUNTRYTAG,'_TITLE')=T3.TAG
LEFT JOIN EN_TEXT AS T4 ON CONCAT(T1.COUNTRYTAG,'_HEADING_%')=T4.TAG AND T4.TAG IS NOT NULL,
WHERE
T1.Type='Country_ENGLAND';









share|improve this question
























  • . . This is what you get for storing data in columns that should actually be in rows. Your EN_TEXT table should have a separate row for each heading -- that is, by rows rather than by columns.
    – Gordon Linoff
    Nov 12 '18 at 12:10










  • Don't you have countrId (a FK) in the EN_TEXT table to connect to Countrys table ?
    – Madhur Bhaiya
    Nov 12 '18 at 12:10










  • What is the expected output? All en_text values as rows?
    – Salman A
    Nov 12 '18 at 12:21
















1












1








1







I have a table which has tags which reference another table in multiple languages. I wrote this query which does the job but isn't elegant and its very long winded:



SELECT DISTINCT
T1.ID as CountryID,
T1.Type AS CountryType,
T2.Text AS CountryText,
T3.Text AS CountryTitle,
T4.Text AS Heading1,
T5.Text AS Heading2,
T6.Text AS Heading3,
T7.Text AS Heading4,
T8.Text AS Heading5,
T9.Text AS Heading6,
T10.Text AS Heading7,
T11.Text AS Heading8,
T12.Text AS Heading9,
T13.Text AS Heading10,
T14.Text AS Heading11,
T15.Text AS Heading12,
T16.Text AS Heading13,
T17.Text AS Heading14,
T18.Text AS Heading15,
T19.Text AS Heading16,
T20.Text AS Heading17,
T21.Text AS Heading18,
T22.Text AS Heading19,
T23.Text AS Heading20
FROM
Countrys AS T1
LEFT JOIN EN_TEXT AS T2 ON T1.DESCRIPTION=T2.TAG
LEFT JOIN EN_TEXT AS T3 ON CONCAT(T1.COUNTRYTAG,'_TITLE')=T3.TAG
LEFT JOIN EN_TEXT AS T4 ON CONCAT(T1.COUNTRYTAG,'_HEADING_1')=T4.TAG
LEFT JOIN EN_TEXT AS T5 ON CONCAT(T1.COUNTRYTAG,'_HEADING_2')=T5.TAG
LEFT JOIN EN_TEXT AS T6 ON CONCAT(T1.COUNTRYTAG,'_HEADING_3')=T6.TAG
LEFT JOIN EN_TEXT AS T7 ON CONCAT(T1.COUNTRYTAG,'_HEADING_4')=T7.TAG
LEFT JOIN EN_TEXT AS T8 ON CONCAT(T1.COUNTRYTAG,'_HEADING_5')=T8.TAG
LEFT JOIN EN_TEXT AS T9 ON CONCAT(T1.COUNTRYTAG,'_HEADING_6')=T9.TAG
LEFT JOIN EN_TEXT AS T10 ON CONCAT(T1.COUNTRYTAG,'_HEADING_7')=T10.TAG
LEFT JOIN EN_TEXT AS T11 ON CONCAT(T1.COUNTRYTAG,'_HEADING_8')=T11.TAG
LEFT JOIN EN_TEXT AS T12 ON CONCAT(T1.COUNTRYTAG,'_HEADING_9')=T12.TAG
LEFT JOIN EN_TEXT AS T13 ON CONCAT(T1.COUNTRYTAG,'_HEADING_10')=T13.TAG
LEFT JOIN EN_TEXT AS T14 ON CONCAT(T1.COUNTRYTAG,'_HEADING_11')=T14.TAG
LEFT JOIN EN_TEXT AS T15 ON CONCAT(T1.COUNTRYTAG,'_HEADING_12')=T15.TAG
LEFT JOIN EN_TEXT AS T16 ON CONCAT(T1.COUNTRYTAG,'_HEADING_13')=T16.TAG
LEFT JOIN EN_TEXT AS T17 ON CONCAT(T1.COUNTRYTAG,'_HEADING_14')=T17.TAG
LEFT JOIN EN_TEXT AS T18 ON CONCAT(T1.COUNTRYTAG,'_HEADING_15')=T18.TAG
LEFT JOIN EN_TEXT AS T19 ON CONCAT(T1.COUNTRYTAG,'_HEADING_16')=T19.TAG
LEFT JOIN EN_TEXT AS T20 ON CONCAT(T1.COUNTRYTAG,'_HEADING_17')=T20.TAG
LEFT JOIN EN_TEXT AS T21 ON CONCAT(T1.COUNTRYTAG,'_HEADING_18')=T21.TAG
LEFT JOIN EN_TEXT AS T22 ON CONCAT(T1.COUNTRYTAG,'_HEADING_19')=T22.TAG
LEFT JOIN EN_TEXT AS T23 ON CONCAT(T1.COUNTRYTAG,'_HEADING_20')=T23.TAG
WHERE
T1.Type='Country_ENGLAND';


Is there a way I can simplify this query so that I don't have to have all those LEFT JOINS? Different countries will have different number of headings so I have to get all possible headings to ensure I get them all.



Something like this - I know this wont work, I'm just giving an example of what I am trying to do.



SELECT DISTINCT
T1.ID as CountryID,
T1.Type AS CountryType,
T2.Text AS CountryText,
T3.Text AS CountryTitle,
T4.Text AS Headings?,

FROM
Countrys AS T1
LEFT JOIN EN_TEXT AS T2 ON T1.DESCRIPTION=T2.TAG
LEFT JOIN EN_TEXT AS T3 ON CONCAT(T1.COUNTRYTAG,'_TITLE')=T3.TAG
LEFT JOIN EN_TEXT AS T4 ON CONCAT(T1.COUNTRYTAG,'_HEADING_%')=T4.TAG AND T4.TAG IS NOT NULL,
WHERE
T1.Type='Country_ENGLAND';









share|improve this question















I have a table which has tags which reference another table in multiple languages. I wrote this query which does the job but isn't elegant and its very long winded:



SELECT DISTINCT
T1.ID as CountryID,
T1.Type AS CountryType,
T2.Text AS CountryText,
T3.Text AS CountryTitle,
T4.Text AS Heading1,
T5.Text AS Heading2,
T6.Text AS Heading3,
T7.Text AS Heading4,
T8.Text AS Heading5,
T9.Text AS Heading6,
T10.Text AS Heading7,
T11.Text AS Heading8,
T12.Text AS Heading9,
T13.Text AS Heading10,
T14.Text AS Heading11,
T15.Text AS Heading12,
T16.Text AS Heading13,
T17.Text AS Heading14,
T18.Text AS Heading15,
T19.Text AS Heading16,
T20.Text AS Heading17,
T21.Text AS Heading18,
T22.Text AS Heading19,
T23.Text AS Heading20
FROM
Countrys AS T1
LEFT JOIN EN_TEXT AS T2 ON T1.DESCRIPTION=T2.TAG
LEFT JOIN EN_TEXT AS T3 ON CONCAT(T1.COUNTRYTAG,'_TITLE')=T3.TAG
LEFT JOIN EN_TEXT AS T4 ON CONCAT(T1.COUNTRYTAG,'_HEADING_1')=T4.TAG
LEFT JOIN EN_TEXT AS T5 ON CONCAT(T1.COUNTRYTAG,'_HEADING_2')=T5.TAG
LEFT JOIN EN_TEXT AS T6 ON CONCAT(T1.COUNTRYTAG,'_HEADING_3')=T6.TAG
LEFT JOIN EN_TEXT AS T7 ON CONCAT(T1.COUNTRYTAG,'_HEADING_4')=T7.TAG
LEFT JOIN EN_TEXT AS T8 ON CONCAT(T1.COUNTRYTAG,'_HEADING_5')=T8.TAG
LEFT JOIN EN_TEXT AS T9 ON CONCAT(T1.COUNTRYTAG,'_HEADING_6')=T9.TAG
LEFT JOIN EN_TEXT AS T10 ON CONCAT(T1.COUNTRYTAG,'_HEADING_7')=T10.TAG
LEFT JOIN EN_TEXT AS T11 ON CONCAT(T1.COUNTRYTAG,'_HEADING_8')=T11.TAG
LEFT JOIN EN_TEXT AS T12 ON CONCAT(T1.COUNTRYTAG,'_HEADING_9')=T12.TAG
LEFT JOIN EN_TEXT AS T13 ON CONCAT(T1.COUNTRYTAG,'_HEADING_10')=T13.TAG
LEFT JOIN EN_TEXT AS T14 ON CONCAT(T1.COUNTRYTAG,'_HEADING_11')=T14.TAG
LEFT JOIN EN_TEXT AS T15 ON CONCAT(T1.COUNTRYTAG,'_HEADING_12')=T15.TAG
LEFT JOIN EN_TEXT AS T16 ON CONCAT(T1.COUNTRYTAG,'_HEADING_13')=T16.TAG
LEFT JOIN EN_TEXT AS T17 ON CONCAT(T1.COUNTRYTAG,'_HEADING_14')=T17.TAG
LEFT JOIN EN_TEXT AS T18 ON CONCAT(T1.COUNTRYTAG,'_HEADING_15')=T18.TAG
LEFT JOIN EN_TEXT AS T19 ON CONCAT(T1.COUNTRYTAG,'_HEADING_16')=T19.TAG
LEFT JOIN EN_TEXT AS T20 ON CONCAT(T1.COUNTRYTAG,'_HEADING_17')=T20.TAG
LEFT JOIN EN_TEXT AS T21 ON CONCAT(T1.COUNTRYTAG,'_HEADING_18')=T21.TAG
LEFT JOIN EN_TEXT AS T22 ON CONCAT(T1.COUNTRYTAG,'_HEADING_19')=T22.TAG
LEFT JOIN EN_TEXT AS T23 ON CONCAT(T1.COUNTRYTAG,'_HEADING_20')=T23.TAG
WHERE
T1.Type='Country_ENGLAND';


Is there a way I can simplify this query so that I don't have to have all those LEFT JOINS? Different countries will have different number of headings so I have to get all possible headings to ensure I get them all.



Something like this - I know this wont work, I'm just giving an example of what I am trying to do.



SELECT DISTINCT
T1.ID as CountryID,
T1.Type AS CountryType,
T2.Text AS CountryText,
T3.Text AS CountryTitle,
T4.Text AS Headings?,

FROM
Countrys AS T1
LEFT JOIN EN_TEXT AS T2 ON T1.DESCRIPTION=T2.TAG
LEFT JOIN EN_TEXT AS T3 ON CONCAT(T1.COUNTRYTAG,'_TITLE')=T3.TAG
LEFT JOIN EN_TEXT AS T4 ON CONCAT(T1.COUNTRYTAG,'_HEADING_%')=T4.TAG AND T4.TAG IS NOT NULL,
WHERE
T1.Type='Country_ENGLAND';






mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 18:44









Salman A

176k66336424




176k66336424










asked Nov 12 '18 at 12:07









John PerczykJohn Perczyk

62




62












  • . . This is what you get for storing data in columns that should actually be in rows. Your EN_TEXT table should have a separate row for each heading -- that is, by rows rather than by columns.
    – Gordon Linoff
    Nov 12 '18 at 12:10










  • Don't you have countrId (a FK) in the EN_TEXT table to connect to Countrys table ?
    – Madhur Bhaiya
    Nov 12 '18 at 12:10










  • What is the expected output? All en_text values as rows?
    – Salman A
    Nov 12 '18 at 12:21




















  • . . This is what you get for storing data in columns that should actually be in rows. Your EN_TEXT table should have a separate row for each heading -- that is, by rows rather than by columns.
    – Gordon Linoff
    Nov 12 '18 at 12:10










  • Don't you have countrId (a FK) in the EN_TEXT table to connect to Countrys table ?
    – Madhur Bhaiya
    Nov 12 '18 at 12:10










  • What is the expected output? All en_text values as rows?
    – Salman A
    Nov 12 '18 at 12:21


















. . This is what you get for storing data in columns that should actually be in rows. Your EN_TEXT table should have a separate row for each heading -- that is, by rows rather than by columns.
– Gordon Linoff
Nov 12 '18 at 12:10




. . This is what you get for storing data in columns that should actually be in rows. Your EN_TEXT table should have a separate row for each heading -- that is, by rows rather than by columns.
– Gordon Linoff
Nov 12 '18 at 12:10












Don't you have countrId (a FK) in the EN_TEXT table to connect to Countrys table ?
– Madhur Bhaiya
Nov 12 '18 at 12:10




Don't you have countrId (a FK) in the EN_TEXT table to connect to Countrys table ?
– Madhur Bhaiya
Nov 12 '18 at 12:10












What is the expected output? All en_text values as rows?
– Salman A
Nov 12 '18 at 12:21






What is the expected output? All en_text values as rows?
– Salman A
Nov 12 '18 at 12:21














2 Answers
2






active

oldest

votes


















2














You could use conditional aggregation to pivot the tags



SELECT DISTINCT
C.ID as CountryID,
C.Type AS CountryType,
MAX(CASE WHEN C.DESCRIPTION = T.TAG THEN T.Text END) AS CountryText,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_TITLE') THEN T.Text END) AS CountryTitle,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_1') THEN T.Text END) AS Heading1,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_2') THEN T.Text END) AS Heading2,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_3') THEN T.Text END) AS Heading3,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_4') THEN T.Text END) AS Heading4,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_5') THEN T.Text END) AS Heading5,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_6') THEN T.Text END) AS Heading6,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_7') THEN T.Text END) AS Heading7,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_8') THEN T.Text END) AS Heading8,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_9') THEN T.Text END) AS Heading9,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_10') THEN T.Text END) AS Heading10,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_11') THEN T.Text END) AS Heading11,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_12') THEN T.Text END) AS Heading12,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_13') THEN T.Text END) AS Heading13,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_14') THEN T.Text END) AS Heading14,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_15') THEN T.Text END) AS Heading15,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_16') THEN T.Text END) AS Heading16,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_17') THEN T.Text END) AS Heading17,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_18') THEN T.Text END) AS Heading18,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_19') THEN T.Text END) AS Heading19,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_20') THEN T.Text END) AS Heading20
FROM Countrys AS C
LEFT JOIN EN_TEXT AS T
ON T.TAG LIKE CONCAT(C.COUNTRYTAG,'%')
-- AND (T.TAG LIKE '%TITLE' OR T.TAG LIKE '%HEADING%')
WHERE C.Type = 'Country_ENGLAND'
GROUP BY C.ID, C.Type;


Well, it's still a bit long winded.

But it only needs 1 join, so it should be faster.



And in the criteria used in the CASE's you could instead use a LIKE.

Because the ON clause already assures the COUNTRYTAG.



F.e.



... MAX(CASE WHEN T.TAG LIKE '%HEADING_1' THEN T.Text END) AS Heading1, ...


But then you'll have to take the calculated risk that in a LIKE an underscore _ is a placeholder for any 1 character.






share|improve this answer























  • I would even bet that there is only a single record for the country via C.Type = 'Country_ENGLAND', so you would not even need the max() or group by. I would ask the original post person, confirm and simplify.
    – DRapp
    Nov 12 '18 at 12:48










  • Thanks LukStorms for your response. I will certainly try this and Salman's below.
    – John Perczyk
    Nov 12 '18 at 12:55










  • @DRapp you are correct there is only one entry , however the country could be, for example Country_Germany etc I'm just trying to get the main query working before I start working with multiple countries.
    – John Perczyk
    Nov 12 '18 at 12:57










  • @DRapp Yes, there's probably only on C.ID per Type. But the grouping is needed to PIVOT the tags.
    – LukStorms
    Nov 12 '18 at 12:58










  • You're right, my bad :) eyes not quite working yet this morning :)
    – DRapp
    Nov 12 '18 at 13:06



















0














Your design looks OK-ish but your query does not. Having one rows per translation is perhaps better than having one row with a dozen columns. Just build a list, or perhaps a table, of all tags you're interested in and left join with it:



SELECT COUNTRYS.ID, COUNTRYS.TYPE, TAGLIST.TAG, EN_TEXT.Text
FROM COUNTRYS
CROSS JOIN (
SELECT 'TITLE' AS TAG
SELECT 'HEADING_1'
SELECT 'HEADING_2'
SELECT 'HEADING_3'
SELECT 'HEADING_4'
SELECT 'HEADING_5'
SELECT 'HEADING_6'
SELECT 'HEADING_7'
SELECT 'HEADING_8'
SELECT 'HEADING_9'
SELECT 'HEADING_10'
SELECT 'HEADING_11'
SELECT 'HEADING_12'
SELECT 'HEADING_13'
SELECT 'HEADING_14'
SELECT 'HEADING_15'
SELECT 'HEADING_16'
SELECT 'HEADING_17'
SELECT 'HEADING_18'
SELECT 'HEADING_19'
SELECT 'HEADING_20'
) AS TAGLIST
LEFT JOIN EN_TEXT ON
EN_TEXT.TAG = COUNTRYS.DESCRIPTION OR
EN_TEXT.TAG = CONCAT(COUNTRYS.COUNTRYTAG, '_', TAGLIST.TAG)
WHERE COUNTRYS.TYPE = 'Country_ENGLAND'


This will give you one row (or may be more if you have duplicates) per tag, or NULL in the text column if matching translation was not found.






share|improve this answer























  • Thanks Salman A, a most interesting response. I will try this way
    – John Perczyk
    Nov 12 '18 at 12:53










  • MySQL doesn't like the CROSS JOIN part and errors if I put that in
    – John Perczyk
    Nov 12 '18 at 13:23










  • Change to INNER. If it complains about missing ON condition then specify ON 1 = 1.
    – Salman A
    Nov 12 '18 at 13:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261880%2fsimplifying-a-mysql-query-with-many-left-joins%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You could use conditional aggregation to pivot the tags



SELECT DISTINCT
C.ID as CountryID,
C.Type AS CountryType,
MAX(CASE WHEN C.DESCRIPTION = T.TAG THEN T.Text END) AS CountryText,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_TITLE') THEN T.Text END) AS CountryTitle,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_1') THEN T.Text END) AS Heading1,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_2') THEN T.Text END) AS Heading2,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_3') THEN T.Text END) AS Heading3,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_4') THEN T.Text END) AS Heading4,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_5') THEN T.Text END) AS Heading5,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_6') THEN T.Text END) AS Heading6,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_7') THEN T.Text END) AS Heading7,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_8') THEN T.Text END) AS Heading8,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_9') THEN T.Text END) AS Heading9,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_10') THEN T.Text END) AS Heading10,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_11') THEN T.Text END) AS Heading11,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_12') THEN T.Text END) AS Heading12,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_13') THEN T.Text END) AS Heading13,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_14') THEN T.Text END) AS Heading14,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_15') THEN T.Text END) AS Heading15,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_16') THEN T.Text END) AS Heading16,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_17') THEN T.Text END) AS Heading17,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_18') THEN T.Text END) AS Heading18,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_19') THEN T.Text END) AS Heading19,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_20') THEN T.Text END) AS Heading20
FROM Countrys AS C
LEFT JOIN EN_TEXT AS T
ON T.TAG LIKE CONCAT(C.COUNTRYTAG,'%')
-- AND (T.TAG LIKE '%TITLE' OR T.TAG LIKE '%HEADING%')
WHERE C.Type = 'Country_ENGLAND'
GROUP BY C.ID, C.Type;


Well, it's still a bit long winded.

But it only needs 1 join, so it should be faster.



And in the criteria used in the CASE's you could instead use a LIKE.

Because the ON clause already assures the COUNTRYTAG.



F.e.



... MAX(CASE WHEN T.TAG LIKE '%HEADING_1' THEN T.Text END) AS Heading1, ...


But then you'll have to take the calculated risk that in a LIKE an underscore _ is a placeholder for any 1 character.






share|improve this answer























  • I would even bet that there is only a single record for the country via C.Type = 'Country_ENGLAND', so you would not even need the max() or group by. I would ask the original post person, confirm and simplify.
    – DRapp
    Nov 12 '18 at 12:48










  • Thanks LukStorms for your response. I will certainly try this and Salman's below.
    – John Perczyk
    Nov 12 '18 at 12:55










  • @DRapp you are correct there is only one entry , however the country could be, for example Country_Germany etc I'm just trying to get the main query working before I start working with multiple countries.
    – John Perczyk
    Nov 12 '18 at 12:57










  • @DRapp Yes, there's probably only on C.ID per Type. But the grouping is needed to PIVOT the tags.
    – LukStorms
    Nov 12 '18 at 12:58










  • You're right, my bad :) eyes not quite working yet this morning :)
    – DRapp
    Nov 12 '18 at 13:06
















2














You could use conditional aggregation to pivot the tags



SELECT DISTINCT
C.ID as CountryID,
C.Type AS CountryType,
MAX(CASE WHEN C.DESCRIPTION = T.TAG THEN T.Text END) AS CountryText,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_TITLE') THEN T.Text END) AS CountryTitle,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_1') THEN T.Text END) AS Heading1,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_2') THEN T.Text END) AS Heading2,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_3') THEN T.Text END) AS Heading3,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_4') THEN T.Text END) AS Heading4,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_5') THEN T.Text END) AS Heading5,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_6') THEN T.Text END) AS Heading6,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_7') THEN T.Text END) AS Heading7,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_8') THEN T.Text END) AS Heading8,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_9') THEN T.Text END) AS Heading9,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_10') THEN T.Text END) AS Heading10,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_11') THEN T.Text END) AS Heading11,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_12') THEN T.Text END) AS Heading12,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_13') THEN T.Text END) AS Heading13,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_14') THEN T.Text END) AS Heading14,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_15') THEN T.Text END) AS Heading15,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_16') THEN T.Text END) AS Heading16,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_17') THEN T.Text END) AS Heading17,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_18') THEN T.Text END) AS Heading18,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_19') THEN T.Text END) AS Heading19,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_20') THEN T.Text END) AS Heading20
FROM Countrys AS C
LEFT JOIN EN_TEXT AS T
ON T.TAG LIKE CONCAT(C.COUNTRYTAG,'%')
-- AND (T.TAG LIKE '%TITLE' OR T.TAG LIKE '%HEADING%')
WHERE C.Type = 'Country_ENGLAND'
GROUP BY C.ID, C.Type;


Well, it's still a bit long winded.

But it only needs 1 join, so it should be faster.



And in the criteria used in the CASE's you could instead use a LIKE.

Because the ON clause already assures the COUNTRYTAG.



F.e.



... MAX(CASE WHEN T.TAG LIKE '%HEADING_1' THEN T.Text END) AS Heading1, ...


But then you'll have to take the calculated risk that in a LIKE an underscore _ is a placeholder for any 1 character.






share|improve this answer























  • I would even bet that there is only a single record for the country via C.Type = 'Country_ENGLAND', so you would not even need the max() or group by. I would ask the original post person, confirm and simplify.
    – DRapp
    Nov 12 '18 at 12:48










  • Thanks LukStorms for your response. I will certainly try this and Salman's below.
    – John Perczyk
    Nov 12 '18 at 12:55










  • @DRapp you are correct there is only one entry , however the country could be, for example Country_Germany etc I'm just trying to get the main query working before I start working with multiple countries.
    – John Perczyk
    Nov 12 '18 at 12:57










  • @DRapp Yes, there's probably only on C.ID per Type. But the grouping is needed to PIVOT the tags.
    – LukStorms
    Nov 12 '18 at 12:58










  • You're right, my bad :) eyes not quite working yet this morning :)
    – DRapp
    Nov 12 '18 at 13:06














2












2








2






You could use conditional aggregation to pivot the tags



SELECT DISTINCT
C.ID as CountryID,
C.Type AS CountryType,
MAX(CASE WHEN C.DESCRIPTION = T.TAG THEN T.Text END) AS CountryText,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_TITLE') THEN T.Text END) AS CountryTitle,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_1') THEN T.Text END) AS Heading1,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_2') THEN T.Text END) AS Heading2,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_3') THEN T.Text END) AS Heading3,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_4') THEN T.Text END) AS Heading4,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_5') THEN T.Text END) AS Heading5,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_6') THEN T.Text END) AS Heading6,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_7') THEN T.Text END) AS Heading7,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_8') THEN T.Text END) AS Heading8,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_9') THEN T.Text END) AS Heading9,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_10') THEN T.Text END) AS Heading10,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_11') THEN T.Text END) AS Heading11,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_12') THEN T.Text END) AS Heading12,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_13') THEN T.Text END) AS Heading13,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_14') THEN T.Text END) AS Heading14,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_15') THEN T.Text END) AS Heading15,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_16') THEN T.Text END) AS Heading16,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_17') THEN T.Text END) AS Heading17,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_18') THEN T.Text END) AS Heading18,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_19') THEN T.Text END) AS Heading19,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_20') THEN T.Text END) AS Heading20
FROM Countrys AS C
LEFT JOIN EN_TEXT AS T
ON T.TAG LIKE CONCAT(C.COUNTRYTAG,'%')
-- AND (T.TAG LIKE '%TITLE' OR T.TAG LIKE '%HEADING%')
WHERE C.Type = 'Country_ENGLAND'
GROUP BY C.ID, C.Type;


Well, it's still a bit long winded.

But it only needs 1 join, so it should be faster.



And in the criteria used in the CASE's you could instead use a LIKE.

Because the ON clause already assures the COUNTRYTAG.



F.e.



... MAX(CASE WHEN T.TAG LIKE '%HEADING_1' THEN T.Text END) AS Heading1, ...


But then you'll have to take the calculated risk that in a LIKE an underscore _ is a placeholder for any 1 character.






share|improve this answer














You could use conditional aggregation to pivot the tags



SELECT DISTINCT
C.ID as CountryID,
C.Type AS CountryType,
MAX(CASE WHEN C.DESCRIPTION = T.TAG THEN T.Text END) AS CountryText,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_TITLE') THEN T.Text END) AS CountryTitle,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_1') THEN T.Text END) AS Heading1,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_2') THEN T.Text END) AS Heading2,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_3') THEN T.Text END) AS Heading3,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_4') THEN T.Text END) AS Heading4,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_5') THEN T.Text END) AS Heading5,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_6') THEN T.Text END) AS Heading6,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_7') THEN T.Text END) AS Heading7,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_8') THEN T.Text END) AS Heading8,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_9') THEN T.Text END) AS Heading9,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_10') THEN T.Text END) AS Heading10,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_11') THEN T.Text END) AS Heading11,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_12') THEN T.Text END) AS Heading12,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_13') THEN T.Text END) AS Heading13,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_14') THEN T.Text END) AS Heading14,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_15') THEN T.Text END) AS Heading15,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_16') THEN T.Text END) AS Heading16,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_17') THEN T.Text END) AS Heading17,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_18') THEN T.Text END) AS Heading18,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_19') THEN T.Text END) AS Heading19,
MAX(CASE WHEN T.TAG = CONCAT(C.COUNTRYTAG,'_HEADING_20') THEN T.Text END) AS Heading20
FROM Countrys AS C
LEFT JOIN EN_TEXT AS T
ON T.TAG LIKE CONCAT(C.COUNTRYTAG,'%')
-- AND (T.TAG LIKE '%TITLE' OR T.TAG LIKE '%HEADING%')
WHERE C.Type = 'Country_ENGLAND'
GROUP BY C.ID, C.Type;


Well, it's still a bit long winded.

But it only needs 1 join, so it should be faster.



And in the criteria used in the CASE's you could instead use a LIKE.

Because the ON clause already assures the COUNTRYTAG.



F.e.



... MAX(CASE WHEN T.TAG LIKE '%HEADING_1' THEN T.Text END) AS Heading1, ...


But then you'll have to take the calculated risk that in a LIKE an underscore _ is a placeholder for any 1 character.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 13:07

























answered Nov 12 '18 at 12:31









LukStormsLukStorms

11.8k31532




11.8k31532












  • I would even bet that there is only a single record for the country via C.Type = 'Country_ENGLAND', so you would not even need the max() or group by. I would ask the original post person, confirm and simplify.
    – DRapp
    Nov 12 '18 at 12:48










  • Thanks LukStorms for your response. I will certainly try this and Salman's below.
    – John Perczyk
    Nov 12 '18 at 12:55










  • @DRapp you are correct there is only one entry , however the country could be, for example Country_Germany etc I'm just trying to get the main query working before I start working with multiple countries.
    – John Perczyk
    Nov 12 '18 at 12:57










  • @DRapp Yes, there's probably only on C.ID per Type. But the grouping is needed to PIVOT the tags.
    – LukStorms
    Nov 12 '18 at 12:58










  • You're right, my bad :) eyes not quite working yet this morning :)
    – DRapp
    Nov 12 '18 at 13:06


















  • I would even bet that there is only a single record for the country via C.Type = 'Country_ENGLAND', so you would not even need the max() or group by. I would ask the original post person, confirm and simplify.
    – DRapp
    Nov 12 '18 at 12:48










  • Thanks LukStorms for your response. I will certainly try this and Salman's below.
    – John Perczyk
    Nov 12 '18 at 12:55










  • @DRapp you are correct there is only one entry , however the country could be, for example Country_Germany etc I'm just trying to get the main query working before I start working with multiple countries.
    – John Perczyk
    Nov 12 '18 at 12:57










  • @DRapp Yes, there's probably only on C.ID per Type. But the grouping is needed to PIVOT the tags.
    – LukStorms
    Nov 12 '18 at 12:58










  • You're right, my bad :) eyes not quite working yet this morning :)
    – DRapp
    Nov 12 '18 at 13:06
















I would even bet that there is only a single record for the country via C.Type = 'Country_ENGLAND', so you would not even need the max() or group by. I would ask the original post person, confirm and simplify.
– DRapp
Nov 12 '18 at 12:48




I would even bet that there is only a single record for the country via C.Type = 'Country_ENGLAND', so you would not even need the max() or group by. I would ask the original post person, confirm and simplify.
– DRapp
Nov 12 '18 at 12:48












Thanks LukStorms for your response. I will certainly try this and Salman's below.
– John Perczyk
Nov 12 '18 at 12:55




Thanks LukStorms for your response. I will certainly try this and Salman's below.
– John Perczyk
Nov 12 '18 at 12:55












@DRapp you are correct there is only one entry , however the country could be, for example Country_Germany etc I'm just trying to get the main query working before I start working with multiple countries.
– John Perczyk
Nov 12 '18 at 12:57




@DRapp you are correct there is only one entry , however the country could be, for example Country_Germany etc I'm just trying to get the main query working before I start working with multiple countries.
– John Perczyk
Nov 12 '18 at 12:57












@DRapp Yes, there's probably only on C.ID per Type. But the grouping is needed to PIVOT the tags.
– LukStorms
Nov 12 '18 at 12:58




@DRapp Yes, there's probably only on C.ID per Type. But the grouping is needed to PIVOT the tags.
– LukStorms
Nov 12 '18 at 12:58












You're right, my bad :) eyes not quite working yet this morning :)
– DRapp
Nov 12 '18 at 13:06




You're right, my bad :) eyes not quite working yet this morning :)
– DRapp
Nov 12 '18 at 13:06













0














Your design looks OK-ish but your query does not. Having one rows per translation is perhaps better than having one row with a dozen columns. Just build a list, or perhaps a table, of all tags you're interested in and left join with it:



SELECT COUNTRYS.ID, COUNTRYS.TYPE, TAGLIST.TAG, EN_TEXT.Text
FROM COUNTRYS
CROSS JOIN (
SELECT 'TITLE' AS TAG
SELECT 'HEADING_1'
SELECT 'HEADING_2'
SELECT 'HEADING_3'
SELECT 'HEADING_4'
SELECT 'HEADING_5'
SELECT 'HEADING_6'
SELECT 'HEADING_7'
SELECT 'HEADING_8'
SELECT 'HEADING_9'
SELECT 'HEADING_10'
SELECT 'HEADING_11'
SELECT 'HEADING_12'
SELECT 'HEADING_13'
SELECT 'HEADING_14'
SELECT 'HEADING_15'
SELECT 'HEADING_16'
SELECT 'HEADING_17'
SELECT 'HEADING_18'
SELECT 'HEADING_19'
SELECT 'HEADING_20'
) AS TAGLIST
LEFT JOIN EN_TEXT ON
EN_TEXT.TAG = COUNTRYS.DESCRIPTION OR
EN_TEXT.TAG = CONCAT(COUNTRYS.COUNTRYTAG, '_', TAGLIST.TAG)
WHERE COUNTRYS.TYPE = 'Country_ENGLAND'


This will give you one row (or may be more if you have duplicates) per tag, or NULL in the text column if matching translation was not found.






share|improve this answer























  • Thanks Salman A, a most interesting response. I will try this way
    – John Perczyk
    Nov 12 '18 at 12:53










  • MySQL doesn't like the CROSS JOIN part and errors if I put that in
    – John Perczyk
    Nov 12 '18 at 13:23










  • Change to INNER. If it complains about missing ON condition then specify ON 1 = 1.
    – Salman A
    Nov 12 '18 at 13:25
















0














Your design looks OK-ish but your query does not. Having one rows per translation is perhaps better than having one row with a dozen columns. Just build a list, or perhaps a table, of all tags you're interested in and left join with it:



SELECT COUNTRYS.ID, COUNTRYS.TYPE, TAGLIST.TAG, EN_TEXT.Text
FROM COUNTRYS
CROSS JOIN (
SELECT 'TITLE' AS TAG
SELECT 'HEADING_1'
SELECT 'HEADING_2'
SELECT 'HEADING_3'
SELECT 'HEADING_4'
SELECT 'HEADING_5'
SELECT 'HEADING_6'
SELECT 'HEADING_7'
SELECT 'HEADING_8'
SELECT 'HEADING_9'
SELECT 'HEADING_10'
SELECT 'HEADING_11'
SELECT 'HEADING_12'
SELECT 'HEADING_13'
SELECT 'HEADING_14'
SELECT 'HEADING_15'
SELECT 'HEADING_16'
SELECT 'HEADING_17'
SELECT 'HEADING_18'
SELECT 'HEADING_19'
SELECT 'HEADING_20'
) AS TAGLIST
LEFT JOIN EN_TEXT ON
EN_TEXT.TAG = COUNTRYS.DESCRIPTION OR
EN_TEXT.TAG = CONCAT(COUNTRYS.COUNTRYTAG, '_', TAGLIST.TAG)
WHERE COUNTRYS.TYPE = 'Country_ENGLAND'


This will give you one row (or may be more if you have duplicates) per tag, or NULL in the text column if matching translation was not found.






share|improve this answer























  • Thanks Salman A, a most interesting response. I will try this way
    – John Perczyk
    Nov 12 '18 at 12:53










  • MySQL doesn't like the CROSS JOIN part and errors if I put that in
    – John Perczyk
    Nov 12 '18 at 13:23










  • Change to INNER. If it complains about missing ON condition then specify ON 1 = 1.
    – Salman A
    Nov 12 '18 at 13:25














0












0








0






Your design looks OK-ish but your query does not. Having one rows per translation is perhaps better than having one row with a dozen columns. Just build a list, or perhaps a table, of all tags you're interested in and left join with it:



SELECT COUNTRYS.ID, COUNTRYS.TYPE, TAGLIST.TAG, EN_TEXT.Text
FROM COUNTRYS
CROSS JOIN (
SELECT 'TITLE' AS TAG
SELECT 'HEADING_1'
SELECT 'HEADING_2'
SELECT 'HEADING_3'
SELECT 'HEADING_4'
SELECT 'HEADING_5'
SELECT 'HEADING_6'
SELECT 'HEADING_7'
SELECT 'HEADING_8'
SELECT 'HEADING_9'
SELECT 'HEADING_10'
SELECT 'HEADING_11'
SELECT 'HEADING_12'
SELECT 'HEADING_13'
SELECT 'HEADING_14'
SELECT 'HEADING_15'
SELECT 'HEADING_16'
SELECT 'HEADING_17'
SELECT 'HEADING_18'
SELECT 'HEADING_19'
SELECT 'HEADING_20'
) AS TAGLIST
LEFT JOIN EN_TEXT ON
EN_TEXT.TAG = COUNTRYS.DESCRIPTION OR
EN_TEXT.TAG = CONCAT(COUNTRYS.COUNTRYTAG, '_', TAGLIST.TAG)
WHERE COUNTRYS.TYPE = 'Country_ENGLAND'


This will give you one row (or may be more if you have duplicates) per tag, or NULL in the text column if matching translation was not found.






share|improve this answer














Your design looks OK-ish but your query does not. Having one rows per translation is perhaps better than having one row with a dozen columns. Just build a list, or perhaps a table, of all tags you're interested in and left join with it:



SELECT COUNTRYS.ID, COUNTRYS.TYPE, TAGLIST.TAG, EN_TEXT.Text
FROM COUNTRYS
CROSS JOIN (
SELECT 'TITLE' AS TAG
SELECT 'HEADING_1'
SELECT 'HEADING_2'
SELECT 'HEADING_3'
SELECT 'HEADING_4'
SELECT 'HEADING_5'
SELECT 'HEADING_6'
SELECT 'HEADING_7'
SELECT 'HEADING_8'
SELECT 'HEADING_9'
SELECT 'HEADING_10'
SELECT 'HEADING_11'
SELECT 'HEADING_12'
SELECT 'HEADING_13'
SELECT 'HEADING_14'
SELECT 'HEADING_15'
SELECT 'HEADING_16'
SELECT 'HEADING_17'
SELECT 'HEADING_18'
SELECT 'HEADING_19'
SELECT 'HEADING_20'
) AS TAGLIST
LEFT JOIN EN_TEXT ON
EN_TEXT.TAG = COUNTRYS.DESCRIPTION OR
EN_TEXT.TAG = CONCAT(COUNTRYS.COUNTRYTAG, '_', TAGLIST.TAG)
WHERE COUNTRYS.TYPE = 'Country_ENGLAND'


This will give you one row (or may be more if you have duplicates) per tag, or NULL in the text column if matching translation was not found.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 13:49

























answered Nov 12 '18 at 12:30









Salman ASalman A

176k66336424




176k66336424












  • Thanks Salman A, a most interesting response. I will try this way
    – John Perczyk
    Nov 12 '18 at 12:53










  • MySQL doesn't like the CROSS JOIN part and errors if I put that in
    – John Perczyk
    Nov 12 '18 at 13:23










  • Change to INNER. If it complains about missing ON condition then specify ON 1 = 1.
    – Salman A
    Nov 12 '18 at 13:25


















  • Thanks Salman A, a most interesting response. I will try this way
    – John Perczyk
    Nov 12 '18 at 12:53










  • MySQL doesn't like the CROSS JOIN part and errors if I put that in
    – John Perczyk
    Nov 12 '18 at 13:23










  • Change to INNER. If it complains about missing ON condition then specify ON 1 = 1.
    – Salman A
    Nov 12 '18 at 13:25
















Thanks Salman A, a most interesting response. I will try this way
– John Perczyk
Nov 12 '18 at 12:53




Thanks Salman A, a most interesting response. I will try this way
– John Perczyk
Nov 12 '18 at 12:53












MySQL doesn't like the CROSS JOIN part and errors if I put that in
– John Perczyk
Nov 12 '18 at 13:23




MySQL doesn't like the CROSS JOIN part and errors if I put that in
– John Perczyk
Nov 12 '18 at 13:23












Change to INNER. If it complains about missing ON condition then specify ON 1 = 1.
– Salman A
Nov 12 '18 at 13:25




Change to INNER. If it complains about missing ON condition then specify ON 1 = 1.
– Salman A
Nov 12 '18 at 13:25


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261880%2fsimplifying-a-mysql-query-with-many-left-joins%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation