Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:43:53+00:00 2026-05-25T20:43:53+00:00

So I am trying to do multiple GROUP_CONCAT() s in a query on the

  • 0

So I am trying to do multiple GROUP_CONCAT()s in a query on the same column with if statements.
Even though the data is there, it is not giving me accurate info. I tried upping the group_concat_max_len, but I still get the same incorrect data.

Here is an example:

SELECT 
    field1,
    field2,
    GROUP_CONCAT(DISTINCT IF(field3 = 2, field4, NULL)) list1,
    GROUP_CONCAT(DISTINCT IF(field3 = 3, field4, NULL)) list2,
    GROUP_CONCAT(DISTINCT IF(field3 = 4, field4, NULL)) list3
FROM table1
GROUP BY field5

The results in the list1, list2 and list3 are not accurate, though the data is. Am I hitting some constraint from doing it this way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:43:54+00:00Added an answer on May 25, 2026 at 8:43 pm
    SELECT field1,field2,last1,list2,list3 FROM
    (
        SELECT
            field1, 
            field2,
            field5,
            GROUP_CONCAT(DISTINCT IF(field3 = 2, field4, 'NULL')) list1, 
            GROUP_CONCAT(DISTINCT IF(field3 = 3, field4, 'NULL')) list2, 
            GROUP_CONCAT(DISTINCT IF(field3 = 4, field4, 'NULL')) list3 
        FROM
            table1 
        GROUP BY
            field1,field2,field5
    ) A;
    

    I changed the SELECT list, changed the GROUP BY, changed NULL to ‘NULL’, put the whole query in a subquery, and pull out of the subquery everything except field5. Please see if this produces the desired result.

    If you want this query to run fast, please add this index:

    ALTER TABLE table1 ADD INDEX (field1,field2,field5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do multiple counts on the same column with different where like
I am trying to load multiple elements with the same name from XML into
I am trying to write query with multiple select subnets in it.But I defined
I'm trying to produce a report that has multiple grouping but does not just
I am trying to create a script to obtain data from a multiple databases
I have a query which I'm trying to group multiple table columns as text,
This is the code giving me issue - I'm trying to update multiple records
I'm using ormlite for Android and I'm trying to get a multiple column unique-constraint.
I'm having some issues with a mysql query. I'm trying to join together multiple
I am trying to embed multiple external websites into one web page. Using an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.