hibernate 具有内连接的Map表创建重复项

amrnrhlw  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(145)

我有一个课程和课程Map表。一个课程可以有多个课程。我创建了一个从计划到课程表(OneToMany)的Map表。但当我执行内部连接时,我得到了重复的数据,这对我来说是意想不到的。我需要根据课程过滤,如查找课程名称,所以需要加入两个表。我对此有两个疑问
1.我用不同的键盘避免重复。我需要连接以过滤课程,是否可以避免重复而不使用DISTINCT关键字,因为DISTINCT可能会影响我的查询性能
1.是否可以在一个查询中以半列分隔的方式选择所有课程,以防导致重复。
我使用Java实体(Hibernate),所以One to Many Relationship正在构建这个开箱即用的查询。

查询

SELECT SeqNo, pgm_id, start_date, end_date, pgm_description, location, timings
From 
PGM_DETAILS T1
INNER JOIN COURSE_DETAILS T2
ON T1.pgm_id=T2.pgm_id
WHERE T1.pgm_id=110 and T2.course_name in('C bascis','Java Basics');

预期效果

SeqNo|pgm_id  |start_date|end_date |pgm_description|location|timings
 ---------------------------------------------------------------------
    1    |   110  | 12-Sep-22|20-Sep-22|My test PGM    | NY     |3-5 PM |C basics;Java Basics;

PGM_DETAILS

SeqNo|pgm_id  |start_date|end_date |pgm_description|location|timings
---------------------------------------------------------------------
1    |   110  | 12-Sep-22|20-Sep-22|My test PGM    | NY     |3-5 PM  
2    |   101  | 14-Oct-22|21-Oct-22|My Second      | NJ     |8-9 AM  
3    |   102  | 21-Aug-22|30-Sep-22|My JPGM        | NK     |3-5 PM  
4    |   103  | 08-Dec-22|29-Dec-22|Summer Pj      | CA     |7-8 AM
5    |   104  | 12-Sep-22|20-Sep-22|My Ny PGM      | FE     |3-5 PM  
6    |   105  | 12-Sep-22|20-Sep-22|My FE PGM      | CE     |1-5 PM  
7    |   106  | 12-Sep-22|20-Sep-22|My IN PGM      | NJ     |4-7 PM

Course_Detail

mapping_id | pgm_id  | course_name
-------------------------------------------
  1        | 110     | C basics
  2        | 110     | Java basics
  3        | 110     | python basics
  4        | 110     | Angular basics
  5        | 101     | PERL basics
  6        | 101     | Linux basics
  7        | 101     | Spring basics
  8        | 101     | React basics
  9        | 101     | Javascript basics
  10       | 102     | Windows basics
  11       | 102     | Linux basics
  12       | 103     | Spring basics
  13       | 104     | React basics
  14       | 105     | Javascript basics
atmip9wb

atmip9wb1#

SELECT SeqNo, pgm_id, start_date, end_date, pgm_description, location, timings,
(  
   SELECT GROUP_CONCAT(mapping_id) 
   FROM COURSE_DETAILS T2
   WHERE T1.pgm_id=T2.pgm_id

) AS MappingIds
From PGM_DETAILS T1

相关问题