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 7607847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:46:08+00:00 2026-05-31T00:46:08+00:00

consider three entities as student, course, subject Below are the associations – student has_many

  • 0

consider three entities as student, course, subject
Below are the associations –

student has_many courses,
student has_many subjects.

Now i want to fetch student records with subject names and course names using mysql group_concat, left join on courses, left join on subjects and group_by student_id.

Problem is that group_concat('subjects.name') as subject_names gives me duplicate entries of subjects but group_concat('students.name') as student_names gives unique names.

Why ??

  • 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-31T00:46:10+00:00Added an answer on May 31, 2026 at 12:46 am

    The 2 left joins are multiplying rows via Cartesian product of the child rows per student

    Example

    • Student 1 has 3 courses and 2 subjects
    • Generates 6 rows for Student 1
    • Gives one course value per subject = each course repeated twice
    • Gives one subject value per course = each subject repeated thrice

    To fix:

    Option 1: Use GROUP_CONCAT(DISTINCT ...) as per MySQL docs

    In MySQL, you can get the concatenated values of expression combinations. To eliminate duplicate values, use the DISTINCT clause.

    Option 2: Use a UNION ALL + derived table

    SELECT
        Student, MAX(CourseConcat), MAX(SubjectConcat)
    FROM
        (
        -- 2 separate SELECTs here
        .. student LEFT JOIN course ...
        UNION ALL
        .. student LEFT JOIN subjects...
        ) T
    GROUP BY
       Student
    

    The 2nd option may be better albeit more complex because you have less intermediate rows to process with DISTINCT

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following three Hibernate entities: public class Car { @OneToMany(fetch = FetchType.LAZY) @Fetch(FetchMode.SUBSELECT)
Consider i am using join on three tables to get a desired result-set... Now
Consider you have 2 classes: Student and Course and a Course can be added
Consider the following scenario: There are three kinds of entities, say Foo , Bar
Consider three tables: one of items, one of tags on those items, and the
Consider these three mysql statements: select * from Users; select id, title, value from
Consider the three following classes: EntityTransformer contains a map associating an Entity with a
Please consider the following three .NET types: I have an interface, an abstract class,
Consider the following Ruby code analyzing a three-byte UTF-8 string: #encoding: utf-8 s =
Consider i have a user table and I have three columns mobilePhone , homePhone

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.