我正在使用 Hibernate 和 PostgreSql。在 hibernate 状态下,我有两个类:
Mobile(id, name, serial no,model_no)
Model(id, description, value)
现在 Model 类的值存储在 Mobile 类 (model_no) 中。此 Mobile 类没有对 Model 类的任何引用。
在模型类中有如下数据:
Modal
======
id description value
1 aaaa 1
2 bbbbb 2
3 ccccc 3
4 ddddd 12
5 eeee 40
此处此值存储在 Mobile 表中。在 Mobile 表中,我将 model_no 设为 0(这不在模型中,我不想将该值放入模型表中,因为如果我放入,我需要更改大量代码。
现在我想要一个查询来得到输出
value description
---- ------------
0 UNKNOWN
1 aaaa
2 bbbbb
3 ccccc
4 ddddd
5 eeee.
像这样。要获取所有 model_nos
我可以使用类似
select modal_no from Mobile
where modal_no in(select value from Modal) or model_no = 0
但这里我还有模型表中的描述。有人可以帮忙吗?
谢谢兄弟的回复,正如我提到的这个 Mobile 表(Hibernate Mobile 类)没有引用 Model 表(在 hibernate Model 类中)。如果我引用了移动课上的模型,那么你的答案将是 100% 正确的。在我的 Mobile 类中,这个 model_no 是整数值。如果我在 hql 中使用你的查询,我会得到像“预期路径”这样的异常。我希望 Hql(或 sql)查询获得 0 值的输出。 我的hibernate Mobile类是这样的
class Mobile {
int id;
int model_no; // this model_no has 0 + modal_no values(1,2,3,12,42).
// some references like
Manufacture manf;
SerialNo serialno;
Customer cust;
getters and setters
}
我的 Hibernate 模型类就像......
class Model{
int id;
String description;
int value; this column has 1,2,3,12,42. it don't have 0.
setters and getters.
}
最佳答案
left join
可以实现:
select Mobile.modal_no
, Modal.description
from Mobile
left join
Modal
on Mobile.model_no = Modal.value
关于postgresql - 如何从两个表中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12546067/