Table 1 Table 2
Query I am using to get first table from my database:
use mydb;
select doc_id,first_name,title,overall_experience_years,address,description_,Price,degree_name from doctor_profile d inner join doctor_education e on d.doc_id=e.doctor_id inner join doc_degree_master m1 on m1.id=e.doc_degree_master_id inner join doctor_specialization s on d.doc_id=s.doctor_id inner join doc_specialization_master m2 on m2.id=s.doc_Specialization_master_id inner join doctor_pricing p on p.doctor_id=d.doc_id;
I want to get data in table 2 form. What query should I write?
This is my model class in springboot:
@Id
private Long Id;
private String Name;
private String Title;
private int OverallExperience;
private BigDecimal Price;
private String Address;
private List<String> Specialities;
private List<String> Degrees;
See aggregation function GROUP_CONCAT(expr)
.
select doc_id
, first_name
, title
, overall_experience_years
, address
, group_concat(distinct description_) as description_
, Price
, group_concat(distinct degree_name) as degree_name
from doctor_profile d
join doctor_education e on d.doc_id = e.doctor_id
join doc_degree_master m1 on m1.id = e.doc_degree_master_id
join doctor_specialization s on d.doc_id = s.doctor_id
join doc_specialization_master m2 on m2.id = s.doc_Specialization_master_id
join doctor_pricing p on p.doctor_id = d.doc_id
group by doc_id
, first_name
, title
, overall_experience_years
, address
, Price
when you want to merge more than 1 values, you can use group_concat() function . But when you want to merge uniq values you must use distinct .
group_concat(distinct tab_name) and use group by other tab_name except (description_ and degree_name)
select doc_id, first_name, title, overall_experience_years, address
, group_concat(distinct description_) as description_ , Price
, group_concat(distinct degree_name) as degree_name
from doctor_profile d
join doctor_education e on d.doc_id = e.doctor_id
join doc_degree_master m1 on m1.id = e.doc_degree_master_id
join doctor_specialization s on d.doc_id = s.doctor_id
join doc_specialization_master m2 on m2.id = s.doc_Specialization_master_id
join doctor_pricing p on p.doctor_id = d.doc_id
group by doc_id, first_name, title, overall_experience_years, address, Price
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.