我使用JPA和mysql。 每个表都有实体类。我有四个表。如何获得外键使用表名
表1:student_table
studentId(PK), studentName
1 jack
2 robert
3 tom
4 smith
表2:roll_table
rollId(PK), studentId(FK)
10001 1
10002 2
10003 3
10004 4
表3:address_table
addressId(PK) City studentId(FK)
1 Washngton 1
2 NewYork1 2
3 Newyork2 3
4 Wasington2 4
表4:contact_table
------------------------------------------------
contactId(pk) phoneNumber email studentId(FK)
------------------------------------------------
----------------------------------------------
基表是 'student_table'。 'studentId'是这张表的主要关键。
其余3个表格已将此studentId用作外键。 共3个表格包含数据。一张桌子没有任何数据。
我需要编写“studentId = 2使用表名和表查询,如果存在其他表中的数据计算。 否则没有任何其他逻辑来获取这些信息。
就像现在的studentId = 2使用两个表。所以结果是*(roll_table,address_table)*
假设接触表有数据与studentId = 2, 那么结果是*(roll_table,address_table,contact_table)*
帮助我。 谢谢提前
了解[SQL连接(http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins .html):你想使用外部连接。 – eggyal