我想从3个不同的表中只选择几列。
SELECT b.name,s.name,t.name
FROM Q_BANK qb
INNER JOIN TOPIC t ON qb.TOPIC_ID=t.ID
INNER JOIN SUBJECT s ON s.ID=t.SUBJECT_ID
INNER JOIN BRANCH b ON b.ID=s.BRANCH_ID
WHERE qb.Q_ID=123
在MySQL Workbench中输出以下内容是正确的。
JPA代码如下
Query q=localEntityManager.createNativeQuery("SELECT b.name,s.name,t.name
FROM Q_BANK qb INNER JOIN TOPIC t ON qb.TOPIC_ID=t.ID
INNER JOIN SUBJECT s ON s.ID=t.SUBJECT_ID
INNER JOIN BRANCH b ON b.ID=s.BRANCH_ID
WHERE qb.Q_ID=:qId").setParameter("qId", questionId);
List<Object[]> result = q.getResultList();
for (Object[] x : result) {
for(Object y:x) {
System.out.println(y);
}
}
输出如下,它不是预期的输出。