[英]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,其中记录记录用户选择的教育水平(对于同一user_id
,它可以有多行),教育,其中存储用户教育描述(每个user_id
仅一行),和存储所有学位的教育水平。
id | user_id | education_id
1 83 1
2 83 2
user_id | description
83 test
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: 这给了我这个:
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? 如果没有,我怎么只能选择一次,所以我可以在用户提交表单后为用户添加数据?
adding desired result 增加期望的结果
user_id | education | description
83 test testing
83 test2
83 test3
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.