z0mbieKale
z0mbieKale

Reputation: 1028

How to select data from multiple tables with limit 1 for one table

I have multiple checkboxes where user can add education data to database. My structure looks following in database: I have three tables, user_education where I keep the record users selected education levels (it can have multiple rows for the same user_id), education, where I store user education description (only one row per user_id), and education levels where I store all the degrees.

user_education

id | user_id | education_id
1    83        1
2    83        2

education_description

user_id | description
83        test

education_levels

education_id | education
1              alusharidus
2              keskharidus
3              Bachelor’s degree
4              Master’s degree

and so on. My question is how can I select all the data for the specific user, but only show the description once?

I tried running this:

select
    user_education.education_id,
    education_levels.education,
    education_description.description
from
    user_education
        join education_description
        left join education_levels
            on user_education.education_id=education_levels.education_id and education_description.user_id = user_education.user_id WHERE user_education.user_id=83;

which gave me this:

enter image description here

and here is the description inserted multiple times, but I would like to only have it once. Is my structure wrong for this type of logic? If not, how can I select it only once, so I can append the data for the user after he submits the form?

EDIT

adding desired result

education_description

user_id | education | description
83        test        testing
83        test2
83        test3

Upvotes: 0

Views: 575

Answers (1)

Harsh Sanghani
Harsh Sanghani

Reputation: 1722

Can you please try this answer :-

select
    user_education.education_id,
    GROUP_CONCAT(education_levels.education separator ' ,'),
    education_description.description
from
    user_education
        join education_description
        left join education_levels
            on user_education.education_id=education_levels.education_id and education_description.user_id = user_education.user_id 
WHERE user_education.user_id=83 
GROUP BY user_education.user_id;

Upvotes: 2

Related Questions