How to Select DISTINCT Name where Dealer_ID in a Table MYSQL, prevent duplicate records












0















I have a MySQL Table, and a table where I do the next Query:
Select Diferent Customer_Name in a Table where Customer_ID=CED130828MJ8.



The goal of that query is that only get a List on Customer_Name and Customer_ID.



Sometimes certains Customer_ID lacks of Customer_Name, (yep, I know about Unique KEY, but, thats not the ISSUE). So, cuz of this I do a CONCAT, IF Customer_Name is Empty, then Customer_Name = Customer_ID



Obiously ID_USER will change.
Thats the Query that I do:



SELECT DISTINCT(
IF(Dealer_Name<>'',
CONCAT(Dealer_Name, '*',Dealer_ID),
CONCAT(Dealer_ID, '*',Dealer_ID)
)
) AS cc FROM received_invoices
WHERE Customer_ID='CED130828MJ8'
GROUP BY (cc)
ORDER BY cc ASC";


And thats how It looks the result of that QUERY:



"BEST BUY LTD CO*BTB052YU96"


And Almost works, I mean almost, because It only choose one Customer_ID of the table, but If a Customer_ID has two ore more records, it Will print, two records.



Let me show you:



"GERARDO GARCIA RIVERA*GARG870805726"
"GERARDO GARSIA RIBERA*GARG870805726"
"JERARDO GARZIA RIVERA*GARG870805726"
"JUAN ANTONIO MUÑOZ*MUGJ540314TV4"
"GUAN ANTONIO MUNIOZ*MUGJ540314TV4"
"JUAN ANTOÑO MUÑIOS*MUGJ540314TV4"
"JUAN HANTOIO MUÑOC*MUGJ540314TV4"


BUT THAT I really Want instead of that is only Get 1 Record per Customer_ID, like:



"GERARDO GARCIA RIVERA*GARG870805726"
"JUAN ANTONIO MUÑOZ*MUGJ540314TV4"


What Do I missing?, what did you suggest?



Thanks in Advance










share|improve this question





























    0















    I have a MySQL Table, and a table where I do the next Query:
    Select Diferent Customer_Name in a Table where Customer_ID=CED130828MJ8.



    The goal of that query is that only get a List on Customer_Name and Customer_ID.



    Sometimes certains Customer_ID lacks of Customer_Name, (yep, I know about Unique KEY, but, thats not the ISSUE). So, cuz of this I do a CONCAT, IF Customer_Name is Empty, then Customer_Name = Customer_ID



    Obiously ID_USER will change.
    Thats the Query that I do:



    SELECT DISTINCT(
    IF(Dealer_Name<>'',
    CONCAT(Dealer_Name, '*',Dealer_ID),
    CONCAT(Dealer_ID, '*',Dealer_ID)
    )
    ) AS cc FROM received_invoices
    WHERE Customer_ID='CED130828MJ8'
    GROUP BY (cc)
    ORDER BY cc ASC";


    And thats how It looks the result of that QUERY:



    "BEST BUY LTD CO*BTB052YU96"


    And Almost works, I mean almost, because It only choose one Customer_ID of the table, but If a Customer_ID has two ore more records, it Will print, two records.



    Let me show you:



    "GERARDO GARCIA RIVERA*GARG870805726"
    "GERARDO GARSIA RIBERA*GARG870805726"
    "JERARDO GARZIA RIVERA*GARG870805726"
    "JUAN ANTONIO MUÑOZ*MUGJ540314TV4"
    "GUAN ANTONIO MUNIOZ*MUGJ540314TV4"
    "JUAN ANTOÑO MUÑIOS*MUGJ540314TV4"
    "JUAN HANTOIO MUÑOC*MUGJ540314TV4"


    BUT THAT I really Want instead of that is only Get 1 Record per Customer_ID, like:



    "GERARDO GARCIA RIVERA*GARG870805726"
    "JUAN ANTONIO MUÑOZ*MUGJ540314TV4"


    What Do I missing?, what did you suggest?



    Thanks in Advance










    share|improve this question



























      0












      0








      0








      I have a MySQL Table, and a table where I do the next Query:
      Select Diferent Customer_Name in a Table where Customer_ID=CED130828MJ8.



      The goal of that query is that only get a List on Customer_Name and Customer_ID.



      Sometimes certains Customer_ID lacks of Customer_Name, (yep, I know about Unique KEY, but, thats not the ISSUE). So, cuz of this I do a CONCAT, IF Customer_Name is Empty, then Customer_Name = Customer_ID



      Obiously ID_USER will change.
      Thats the Query that I do:



      SELECT DISTINCT(
      IF(Dealer_Name<>'',
      CONCAT(Dealer_Name, '*',Dealer_ID),
      CONCAT(Dealer_ID, '*',Dealer_ID)
      )
      ) AS cc FROM received_invoices
      WHERE Customer_ID='CED130828MJ8'
      GROUP BY (cc)
      ORDER BY cc ASC";


      And thats how It looks the result of that QUERY:



      "BEST BUY LTD CO*BTB052YU96"


      And Almost works, I mean almost, because It only choose one Customer_ID of the table, but If a Customer_ID has two ore more records, it Will print, two records.



      Let me show you:



      "GERARDO GARCIA RIVERA*GARG870805726"
      "GERARDO GARSIA RIBERA*GARG870805726"
      "JERARDO GARZIA RIVERA*GARG870805726"
      "JUAN ANTONIO MUÑOZ*MUGJ540314TV4"
      "GUAN ANTONIO MUNIOZ*MUGJ540314TV4"
      "JUAN ANTOÑO MUÑIOS*MUGJ540314TV4"
      "JUAN HANTOIO MUÑOC*MUGJ540314TV4"


      BUT THAT I really Want instead of that is only Get 1 Record per Customer_ID, like:



      "GERARDO GARCIA RIVERA*GARG870805726"
      "JUAN ANTONIO MUÑOZ*MUGJ540314TV4"


      What Do I missing?, what did you suggest?



      Thanks in Advance










      share|improve this question
















      I have a MySQL Table, and a table where I do the next Query:
      Select Diferent Customer_Name in a Table where Customer_ID=CED130828MJ8.



      The goal of that query is that only get a List on Customer_Name and Customer_ID.



      Sometimes certains Customer_ID lacks of Customer_Name, (yep, I know about Unique KEY, but, thats not the ISSUE). So, cuz of this I do a CONCAT, IF Customer_Name is Empty, then Customer_Name = Customer_ID



      Obiously ID_USER will change.
      Thats the Query that I do:



      SELECT DISTINCT(
      IF(Dealer_Name<>'',
      CONCAT(Dealer_Name, '*',Dealer_ID),
      CONCAT(Dealer_ID, '*',Dealer_ID)
      )
      ) AS cc FROM received_invoices
      WHERE Customer_ID='CED130828MJ8'
      GROUP BY (cc)
      ORDER BY cc ASC";


      And thats how It looks the result of that QUERY:



      "BEST BUY LTD CO*BTB052YU96"


      And Almost works, I mean almost, because It only choose one Customer_ID of the table, but If a Customer_ID has two ore more records, it Will print, two records.



      Let me show you:



      "GERARDO GARCIA RIVERA*GARG870805726"
      "GERARDO GARSIA RIBERA*GARG870805726"
      "JERARDO GARZIA RIVERA*GARG870805726"
      "JUAN ANTONIO MUÑOZ*MUGJ540314TV4"
      "GUAN ANTONIO MUNIOZ*MUGJ540314TV4"
      "JUAN ANTOÑO MUÑIOS*MUGJ540314TV4"
      "JUAN HANTOIO MUÑOC*MUGJ540314TV4"


      BUT THAT I really Want instead of that is only Get 1 Record per Customer_ID, like:



      "GERARDO GARCIA RIVERA*GARG870805726"
      "JUAN ANTONIO MUÑOZ*MUGJ540314TV4"


      What Do I missing?, what did you suggest?



      Thanks in Advance







      mysql duplicates record






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 8:16









      Madhur Bhaiya

      19.6k62236




      19.6k62236










      asked Nov 28 '18 at 8:14









      Gerardo GarcíaGerardo García

      65




      65
























          3 Answers
          3






          active

          oldest

          votes


















          0














          SQL sees concatenated strings as different:

          "GERARDO GARCIA RIVERA*GARG870805726"
          "GERARDO GARSIA RIBERA*GARG870805726"
          "JERARDO GARZIA RIVERA*GARG870805726"



          If you only want to see one entry per Customer_ID, you will also get one name, not all of them. If it does not matter which one will be displayed, then use min() on it:

          select
          if(Dealer_Name<>'',
          concat(min(Dealer_Name), '*',Dealer_ID),
          concat(Dealer_ID, '*',Dealer_ID)
          )
          as cc from received_invoices
          where Customer_ID='CED130828MJ8'
          group by Dealer_ID
          order by cc asc";



          Note, that grouping is now done by Dealer_ID, not full concatenated string.






          share|improve this answer


























          • I tested it, but Throws me that error: "Error code. Cant group on 'cc' " so, then when I delete the "min()" on "Dealer_Name" it shows me the results, but as always, with more than one Dealer_Name per Dealer_ID

            – Gerardo García
            Nov 28 '18 at 15:50













          • Change grouping to group by Dealer_ID.

            – Robertas Murza
            Nov 29 '18 at 6:53



















          0














          Few observations:





          • DISTINCT is not a function. So you don't need to use parentheses around it.


          • DISTINCT with GROUP BY on same column/alias is not required.


          Try the following instead:



          SELECT 
          CONCAT(IF(MAX(Dealer_Name) <> '', MAX(Dealer_Name), Dealer_ID),
          '*',
          Dealer_ID
          ) AS cc
          FROM received_invoices
          WHERE Customer_ID = 'CED130828MJ8'
          GROUP BY Dealer_ID
          ORDER BY cc ASC





          share|improve this answer


























          • It sounds logical, not work for me, the MySQL Workbench thows this: "Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

            – Gerardo García
            Nov 28 '18 at 15:37











          • @GerardoGarcía it seems that you are not running my query yet. Because if you check closely, I dont have GROUP BY in my query

            – Madhur Bhaiya
            Nov 28 '18 at 15:38











          • Yes you're right, Now I tested your query, it doesnt thow errors, but have the same result: i.ibb.co/NY3jQLp/…

            – Gerardo García
            Nov 28 '18 at 16:14













          • @GerardoGarcía check carefully there are no same results. Some have additional spaces or . between them

            – Madhur Bhaiya
            Nov 28 '18 at 16:19











          • yes Im Ok with you. The Idea is Only shows One Dealer Name per Dealer ID, as you can see on the picture, and I'm agree with you some have additional spaces or . between them. For example on "ACO960619AY9", it throws two results: "ACEROS Y CORRUGADOS SA DE CV" and "ACEROS Y CORRUGADOS SA. DE CV." But Only need to get One result

            – Gerardo García
            Nov 28 '18 at 16:44



















          0














          @Madhur Bhaiya.



          Before All, Thanks for your Attention, Your great, As you Said, that is the answer:



          SELECT 
          CONCAT(IF(MAX(Dealer_Name) <> '', MAX(Dealer_Name), Dealer_ID),
          '*',
          Dealer_ID
          ) AS cc
          FROM received_invoices
          WHERE Customer_ID = 'CED130828MJ8'
          GROUP BY Dealer_ID
          ORDER BY cc ASC


          In my particular case, might be not in other people, when I do that query it throws me that Error: Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by



          So I google about it, and I found that when this kind of error happens, I must DISABLE-ONLY-FULL-GROUP-BY.



          And in order to do that I need to modify my /etc/mysql/my.cnf Cuz I have Ubuntu Linux I do it at this way:




          1. sudo nano /etc/mysql/my.cnf

          2. Add this to the end of the file

          3. [mysqld]
            sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" 4. For Restart the MySQL server and apply the changes: "sudo service mysql restart" THIS WILL DISABLE ONLY_FULL_GROUP_BY for ALL users.


          Then Do the Query Again, and Voila!!, The Query Works ok!!!!.



          So now I need to Modify the my.cnf file after installed mysql on every platform of my customers (Windows/Linux/MAC).



          In order to avoid that Errors.






          share|improve this answer























            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%2f53514888%2fhow-to-select-distinct-name-where-dealer-id-in-a-table-mysql-prevent-duplicate%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            SQL sees concatenated strings as different:

            "GERARDO GARCIA RIVERA*GARG870805726"
            "GERARDO GARSIA RIBERA*GARG870805726"
            "JERARDO GARZIA RIVERA*GARG870805726"



            If you only want to see one entry per Customer_ID, you will also get one name, not all of them. If it does not matter which one will be displayed, then use min() on it:

            select
            if(Dealer_Name<>'',
            concat(min(Dealer_Name), '*',Dealer_ID),
            concat(Dealer_ID, '*',Dealer_ID)
            )
            as cc from received_invoices
            where Customer_ID='CED130828MJ8'
            group by Dealer_ID
            order by cc asc";



            Note, that grouping is now done by Dealer_ID, not full concatenated string.






            share|improve this answer


























            • I tested it, but Throws me that error: "Error code. Cant group on 'cc' " so, then when I delete the "min()" on "Dealer_Name" it shows me the results, but as always, with more than one Dealer_Name per Dealer_ID

              – Gerardo García
              Nov 28 '18 at 15:50













            • Change grouping to group by Dealer_ID.

              – Robertas Murza
              Nov 29 '18 at 6:53
















            0














            SQL sees concatenated strings as different:

            "GERARDO GARCIA RIVERA*GARG870805726"
            "GERARDO GARSIA RIBERA*GARG870805726"
            "JERARDO GARZIA RIVERA*GARG870805726"



            If you only want to see one entry per Customer_ID, you will also get one name, not all of them. If it does not matter which one will be displayed, then use min() on it:

            select
            if(Dealer_Name<>'',
            concat(min(Dealer_Name), '*',Dealer_ID),
            concat(Dealer_ID, '*',Dealer_ID)
            )
            as cc from received_invoices
            where Customer_ID='CED130828MJ8'
            group by Dealer_ID
            order by cc asc";



            Note, that grouping is now done by Dealer_ID, not full concatenated string.






            share|improve this answer


























            • I tested it, but Throws me that error: "Error code. Cant group on 'cc' " so, then when I delete the "min()" on "Dealer_Name" it shows me the results, but as always, with more than one Dealer_Name per Dealer_ID

              – Gerardo García
              Nov 28 '18 at 15:50













            • Change grouping to group by Dealer_ID.

              – Robertas Murza
              Nov 29 '18 at 6:53














            0












            0








            0







            SQL sees concatenated strings as different:

            "GERARDO GARCIA RIVERA*GARG870805726"
            "GERARDO GARSIA RIBERA*GARG870805726"
            "JERARDO GARZIA RIVERA*GARG870805726"



            If you only want to see one entry per Customer_ID, you will also get one name, not all of them. If it does not matter which one will be displayed, then use min() on it:

            select
            if(Dealer_Name<>'',
            concat(min(Dealer_Name), '*',Dealer_ID),
            concat(Dealer_ID, '*',Dealer_ID)
            )
            as cc from received_invoices
            where Customer_ID='CED130828MJ8'
            group by Dealer_ID
            order by cc asc";



            Note, that grouping is now done by Dealer_ID, not full concatenated string.






            share|improve this answer















            SQL sees concatenated strings as different:

            "GERARDO GARCIA RIVERA*GARG870805726"
            "GERARDO GARSIA RIBERA*GARG870805726"
            "JERARDO GARZIA RIVERA*GARG870805726"



            If you only want to see one entry per Customer_ID, you will also get one name, not all of them. If it does not matter which one will be displayed, then use min() on it:

            select
            if(Dealer_Name<>'',
            concat(min(Dealer_Name), '*',Dealer_ID),
            concat(Dealer_ID, '*',Dealer_ID)
            )
            as cc from received_invoices
            where Customer_ID='CED130828MJ8'
            group by Dealer_ID
            order by cc asc";



            Note, that grouping is now done by Dealer_ID, not full concatenated string.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 28 '18 at 9:52

























            answered Nov 28 '18 at 9:42









            Robertas MurzaRobertas Murza

            463




            463













            • I tested it, but Throws me that error: "Error code. Cant group on 'cc' " so, then when I delete the "min()" on "Dealer_Name" it shows me the results, but as always, with more than one Dealer_Name per Dealer_ID

              – Gerardo García
              Nov 28 '18 at 15:50













            • Change grouping to group by Dealer_ID.

              – Robertas Murza
              Nov 29 '18 at 6:53



















            • I tested it, but Throws me that error: "Error code. Cant group on 'cc' " so, then when I delete the "min()" on "Dealer_Name" it shows me the results, but as always, with more than one Dealer_Name per Dealer_ID

              – Gerardo García
              Nov 28 '18 at 15:50













            • Change grouping to group by Dealer_ID.

              – Robertas Murza
              Nov 29 '18 at 6:53

















            I tested it, but Throws me that error: "Error code. Cant group on 'cc' " so, then when I delete the "min()" on "Dealer_Name" it shows me the results, but as always, with more than one Dealer_Name per Dealer_ID

            – Gerardo García
            Nov 28 '18 at 15:50







            I tested it, but Throws me that error: "Error code. Cant group on 'cc' " so, then when I delete the "min()" on "Dealer_Name" it shows me the results, but as always, with more than one Dealer_Name per Dealer_ID

            – Gerardo García
            Nov 28 '18 at 15:50















            Change grouping to group by Dealer_ID.

            – Robertas Murza
            Nov 29 '18 at 6:53





            Change grouping to group by Dealer_ID.

            – Robertas Murza
            Nov 29 '18 at 6:53













            0














            Few observations:





            • DISTINCT is not a function. So you don't need to use parentheses around it.


            • DISTINCT with GROUP BY on same column/alias is not required.


            Try the following instead:



            SELECT 
            CONCAT(IF(MAX(Dealer_Name) <> '', MAX(Dealer_Name), Dealer_ID),
            '*',
            Dealer_ID
            ) AS cc
            FROM received_invoices
            WHERE Customer_ID = 'CED130828MJ8'
            GROUP BY Dealer_ID
            ORDER BY cc ASC





            share|improve this answer


























            • It sounds logical, not work for me, the MySQL Workbench thows this: "Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

              – Gerardo García
              Nov 28 '18 at 15:37











            • @GerardoGarcía it seems that you are not running my query yet. Because if you check closely, I dont have GROUP BY in my query

              – Madhur Bhaiya
              Nov 28 '18 at 15:38











            • Yes you're right, Now I tested your query, it doesnt thow errors, but have the same result: i.ibb.co/NY3jQLp/…

              – Gerardo García
              Nov 28 '18 at 16:14













            • @GerardoGarcía check carefully there are no same results. Some have additional spaces or . between them

              – Madhur Bhaiya
              Nov 28 '18 at 16:19











            • yes Im Ok with you. The Idea is Only shows One Dealer Name per Dealer ID, as you can see on the picture, and I'm agree with you some have additional spaces or . between them. For example on "ACO960619AY9", it throws two results: "ACEROS Y CORRUGADOS SA DE CV" and "ACEROS Y CORRUGADOS SA. DE CV." But Only need to get One result

              – Gerardo García
              Nov 28 '18 at 16:44
















            0














            Few observations:





            • DISTINCT is not a function. So you don't need to use parentheses around it.


            • DISTINCT with GROUP BY on same column/alias is not required.


            Try the following instead:



            SELECT 
            CONCAT(IF(MAX(Dealer_Name) <> '', MAX(Dealer_Name), Dealer_ID),
            '*',
            Dealer_ID
            ) AS cc
            FROM received_invoices
            WHERE Customer_ID = 'CED130828MJ8'
            GROUP BY Dealer_ID
            ORDER BY cc ASC





            share|improve this answer


























            • It sounds logical, not work for me, the MySQL Workbench thows this: "Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

              – Gerardo García
              Nov 28 '18 at 15:37











            • @GerardoGarcía it seems that you are not running my query yet. Because if you check closely, I dont have GROUP BY in my query

              – Madhur Bhaiya
              Nov 28 '18 at 15:38











            • Yes you're right, Now I tested your query, it doesnt thow errors, but have the same result: i.ibb.co/NY3jQLp/…

              – Gerardo García
              Nov 28 '18 at 16:14













            • @GerardoGarcía check carefully there are no same results. Some have additional spaces or . between them

              – Madhur Bhaiya
              Nov 28 '18 at 16:19











            • yes Im Ok with you. The Idea is Only shows One Dealer Name per Dealer ID, as you can see on the picture, and I'm agree with you some have additional spaces or . between them. For example on "ACO960619AY9", it throws two results: "ACEROS Y CORRUGADOS SA DE CV" and "ACEROS Y CORRUGADOS SA. DE CV." But Only need to get One result

              – Gerardo García
              Nov 28 '18 at 16:44














            0












            0








            0







            Few observations:





            • DISTINCT is not a function. So you don't need to use parentheses around it.


            • DISTINCT with GROUP BY on same column/alias is not required.


            Try the following instead:



            SELECT 
            CONCAT(IF(MAX(Dealer_Name) <> '', MAX(Dealer_Name), Dealer_ID),
            '*',
            Dealer_ID
            ) AS cc
            FROM received_invoices
            WHERE Customer_ID = 'CED130828MJ8'
            GROUP BY Dealer_ID
            ORDER BY cc ASC





            share|improve this answer















            Few observations:





            • DISTINCT is not a function. So you don't need to use parentheses around it.


            • DISTINCT with GROUP BY on same column/alias is not required.


            Try the following instead:



            SELECT 
            CONCAT(IF(MAX(Dealer_Name) <> '', MAX(Dealer_Name), Dealer_ID),
            '*',
            Dealer_ID
            ) AS cc
            FROM received_invoices
            WHERE Customer_ID = 'CED130828MJ8'
            GROUP BY Dealer_ID
            ORDER BY cc ASC






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 28 '18 at 16:47

























            answered Nov 28 '18 at 8:21









            Madhur BhaiyaMadhur Bhaiya

            19.6k62236




            19.6k62236













            • It sounds logical, not work for me, the MySQL Workbench thows this: "Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

              – Gerardo García
              Nov 28 '18 at 15:37











            • @GerardoGarcía it seems that you are not running my query yet. Because if you check closely, I dont have GROUP BY in my query

              – Madhur Bhaiya
              Nov 28 '18 at 15:38











            • Yes you're right, Now I tested your query, it doesnt thow errors, but have the same result: i.ibb.co/NY3jQLp/…

              – Gerardo García
              Nov 28 '18 at 16:14













            • @GerardoGarcía check carefully there are no same results. Some have additional spaces or . between them

              – Madhur Bhaiya
              Nov 28 '18 at 16:19











            • yes Im Ok with you. The Idea is Only shows One Dealer Name per Dealer ID, as you can see on the picture, and I'm agree with you some have additional spaces or . between them. For example on "ACO960619AY9", it throws two results: "ACEROS Y CORRUGADOS SA DE CV" and "ACEROS Y CORRUGADOS SA. DE CV." But Only need to get One result

              – Gerardo García
              Nov 28 '18 at 16:44



















            • It sounds logical, not work for me, the MySQL Workbench thows this: "Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

              – Gerardo García
              Nov 28 '18 at 15:37











            • @GerardoGarcía it seems that you are not running my query yet. Because if you check closely, I dont have GROUP BY in my query

              – Madhur Bhaiya
              Nov 28 '18 at 15:38











            • Yes you're right, Now I tested your query, it doesnt thow errors, but have the same result: i.ibb.co/NY3jQLp/…

              – Gerardo García
              Nov 28 '18 at 16:14













            • @GerardoGarcía check carefully there are no same results. Some have additional spaces or . between them

              – Madhur Bhaiya
              Nov 28 '18 at 16:19











            • yes Im Ok with you. The Idea is Only shows One Dealer Name per Dealer ID, as you can see on the picture, and I'm agree with you some have additional spaces or . between them. For example on "ACO960619AY9", it throws two results: "ACEROS Y CORRUGADOS SA DE CV" and "ACEROS Y CORRUGADOS SA. DE CV." But Only need to get One result

              – Gerardo García
              Nov 28 '18 at 16:44

















            It sounds logical, not work for me, the MySQL Workbench thows this: "Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

            – Gerardo García
            Nov 28 '18 at 15:37





            It sounds logical, not work for me, the MySQL Workbench thows this: "Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

            – Gerardo García
            Nov 28 '18 at 15:37













            @GerardoGarcía it seems that you are not running my query yet. Because if you check closely, I dont have GROUP BY in my query

            – Madhur Bhaiya
            Nov 28 '18 at 15:38





            @GerardoGarcía it seems that you are not running my query yet. Because if you check closely, I dont have GROUP BY in my query

            – Madhur Bhaiya
            Nov 28 '18 at 15:38













            Yes you're right, Now I tested your query, it doesnt thow errors, but have the same result: i.ibb.co/NY3jQLp/…

            – Gerardo García
            Nov 28 '18 at 16:14







            Yes you're right, Now I tested your query, it doesnt thow errors, but have the same result: i.ibb.co/NY3jQLp/…

            – Gerardo García
            Nov 28 '18 at 16:14















            @GerardoGarcía check carefully there are no same results. Some have additional spaces or . between them

            – Madhur Bhaiya
            Nov 28 '18 at 16:19





            @GerardoGarcía check carefully there are no same results. Some have additional spaces or . between them

            – Madhur Bhaiya
            Nov 28 '18 at 16:19













            yes Im Ok with you. The Idea is Only shows One Dealer Name per Dealer ID, as you can see on the picture, and I'm agree with you some have additional spaces or . between them. For example on "ACO960619AY9", it throws two results: "ACEROS Y CORRUGADOS SA DE CV" and "ACEROS Y CORRUGADOS SA. DE CV." But Only need to get One result

            – Gerardo García
            Nov 28 '18 at 16:44





            yes Im Ok with you. The Idea is Only shows One Dealer Name per Dealer ID, as you can see on the picture, and I'm agree with you some have additional spaces or . between them. For example on "ACO960619AY9", it throws two results: "ACEROS Y CORRUGADOS SA DE CV" and "ACEROS Y CORRUGADOS SA. DE CV." But Only need to get One result

            – Gerardo García
            Nov 28 '18 at 16:44











            0














            @Madhur Bhaiya.



            Before All, Thanks for your Attention, Your great, As you Said, that is the answer:



            SELECT 
            CONCAT(IF(MAX(Dealer_Name) <> '', MAX(Dealer_Name), Dealer_ID),
            '*',
            Dealer_ID
            ) AS cc
            FROM received_invoices
            WHERE Customer_ID = 'CED130828MJ8'
            GROUP BY Dealer_ID
            ORDER BY cc ASC


            In my particular case, might be not in other people, when I do that query it throws me that Error: Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by



            So I google about it, and I found that when this kind of error happens, I must DISABLE-ONLY-FULL-GROUP-BY.



            And in order to do that I need to modify my /etc/mysql/my.cnf Cuz I have Ubuntu Linux I do it at this way:




            1. sudo nano /etc/mysql/my.cnf

            2. Add this to the end of the file

            3. [mysqld]
              sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" 4. For Restart the MySQL server and apply the changes: "sudo service mysql restart" THIS WILL DISABLE ONLY_FULL_GROUP_BY for ALL users.


            Then Do the Query Again, and Voila!!, The Query Works ok!!!!.



            So now I need to Modify the my.cnf file after installed mysql on every platform of my customers (Windows/Linux/MAC).



            In order to avoid that Errors.






            share|improve this answer




























              0














              @Madhur Bhaiya.



              Before All, Thanks for your Attention, Your great, As you Said, that is the answer:



              SELECT 
              CONCAT(IF(MAX(Dealer_Name) <> '', MAX(Dealer_Name), Dealer_ID),
              '*',
              Dealer_ID
              ) AS cc
              FROM received_invoices
              WHERE Customer_ID = 'CED130828MJ8'
              GROUP BY Dealer_ID
              ORDER BY cc ASC


              In my particular case, might be not in other people, when I do that query it throws me that Error: Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by



              So I google about it, and I found that when this kind of error happens, I must DISABLE-ONLY-FULL-GROUP-BY.



              And in order to do that I need to modify my /etc/mysql/my.cnf Cuz I have Ubuntu Linux I do it at this way:




              1. sudo nano /etc/mysql/my.cnf

              2. Add this to the end of the file

              3. [mysqld]
                sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" 4. For Restart the MySQL server and apply the changes: "sudo service mysql restart" THIS WILL DISABLE ONLY_FULL_GROUP_BY for ALL users.


              Then Do the Query Again, and Voila!!, The Query Works ok!!!!.



              So now I need to Modify the my.cnf file after installed mysql on every platform of my customers (Windows/Linux/MAC).



              In order to avoid that Errors.






              share|improve this answer


























                0












                0








                0







                @Madhur Bhaiya.



                Before All, Thanks for your Attention, Your great, As you Said, that is the answer:



                SELECT 
                CONCAT(IF(MAX(Dealer_Name) <> '', MAX(Dealer_Name), Dealer_ID),
                '*',
                Dealer_ID
                ) AS cc
                FROM received_invoices
                WHERE Customer_ID = 'CED130828MJ8'
                GROUP BY Dealer_ID
                ORDER BY cc ASC


                In my particular case, might be not in other people, when I do that query it throws me that Error: Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by



                So I google about it, and I found that when this kind of error happens, I must DISABLE-ONLY-FULL-GROUP-BY.



                And in order to do that I need to modify my /etc/mysql/my.cnf Cuz I have Ubuntu Linux I do it at this way:




                1. sudo nano /etc/mysql/my.cnf

                2. Add this to the end of the file

                3. [mysqld]
                  sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" 4. For Restart the MySQL server and apply the changes: "sudo service mysql restart" THIS WILL DISABLE ONLY_FULL_GROUP_BY for ALL users.


                Then Do the Query Again, and Voila!!, The Query Works ok!!!!.



                So now I need to Modify the my.cnf file after installed mysql on every platform of my customers (Windows/Linux/MAC).



                In order to avoid that Errors.






                share|improve this answer













                @Madhur Bhaiya.



                Before All, Thanks for your Attention, Your great, As you Said, that is the answer:



                SELECT 
                CONCAT(IF(MAX(Dealer_Name) <> '', MAX(Dealer_Name), Dealer_ID),
                '*',
                Dealer_ID
                ) AS cc
                FROM received_invoices
                WHERE Customer_ID = 'CED130828MJ8'
                GROUP BY Dealer_ID
                ORDER BY cc ASC


                In my particular case, might be not in other people, when I do that query it throws me that Error: Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'facturacion.facturas_recibidas.Emisor_Razon_Social' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by



                So I google about it, and I found that when this kind of error happens, I must DISABLE-ONLY-FULL-GROUP-BY.



                And in order to do that I need to modify my /etc/mysql/my.cnf Cuz I have Ubuntu Linux I do it at this way:




                1. sudo nano /etc/mysql/my.cnf

                2. Add this to the end of the file

                3. [mysqld]
                  sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" 4. For Restart the MySQL server and apply the changes: "sudo service mysql restart" THIS WILL DISABLE ONLY_FULL_GROUP_BY for ALL users.


                Then Do the Query Again, and Voila!!, The Query Works ok!!!!.



                So now I need to Modify the my.cnf file after installed mysql on every platform of my customers (Windows/Linux/MAC).



                In order to avoid that Errors.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 28 '18 at 17:57









                Gerardo GarcíaGerardo García

                65




                65






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53514888%2fhow-to-select-distinct-name-where-dealer-id-in-a-table-mysql-prevent-duplicate%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

                    Lallio

                    Futebolista

                    Jornalista