Paul
Paul

Reputation:

How can I get data from two tables?

I am using Hibernate and PostgreSql. In hibernate I have two classes like:

Mobile(id, name, serial no,model_no)
Model(id, description, value)

Now Model class's values are stored in Mobile class (model_no). This Mobile class doesn't have any reference to Model class.

In Model class has data like:

Modal
======
id description value
1  aaaa        1
2  bbbbb       2
3  ccccc       3
4  ddddd       12
5  eeee        40

Here this value is being stored in Mobile table. In Mobile table I have model_no as 0 (which is not in Model, I don't want to put that value in Model table, because if I put I needs to change lot of code.

Now what I want a query to get the out put like

value description
----   ------------
0     UNKNOWN
1     aaaa
2     bbbbb
3     ccccc
4     ddddd
5     eeee.

like this. To get the all model_nos I can use a query like

select modal_no from Mobile 
where modal_no in(select value from Modal) or model_no = 0

but here I what the description also which is in Model table. Can anybody help?


Thank u bro for your response, as i mentioned this Mobile table(Hibernate Mobile class) don't have reference to the Model table(in hibernate Model class). If i have reference to Model in Mobile class then your answer will be 100% correct. In my Mobile class this model_no is integer value. If i use your query in hql i will get the exception like "path expected". I want Hql(or sql) query to get the output with 0 value. my hibernate Mobile class is like this

 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 
}

My Hibernate Model class is like ...

class Model{
   int id;
   String description;
   int value; this column has 1,2,3,12,42. it don't have 0.

   setters and getters.
}

Upvotes: 1

Views: 1307

Answers (1)

Andomar
Andomar

Reputation: 238116

A left join would accomplish that:

select  Mobile.modal_no 
,       Modal.description
from    Mobile 
left join    
        Modal
on      Mobile.model_no = Modal.value

Upvotes: 2

Related Questions