Is this mysql query right or wrong?
"select full_name from user u join user_contact_info ui on ui.user_id=u.user_id,
ui.user_contact_state, ui.user_contact_city_town, ui.user_contact_country where
u.full_name like '"+keyWord+"%'" + "and u.user_id = "+id;
from my user u table I want the fullname and from my user_contac开发者_JAVA技巧t_info table ui i want the state city and country.
Am i writing the join query to get all the values based on the id. I want to know whether am writing the query correctly or not. If wrong How should i write it. Please help me writing join query.
Also in result set how to get the values if i am using a single table I will write my rs as
if(rs.next()){
User user = new User();
user.setFullname(rs.get(full_name);
}
In case of using a second join table how to get and set the values in my object.
"select u.full_name, ui.user_contact_state, ui.user_contact_city_town, ui.user_contact_country
from user u
left outer join user_contact_info ui on ui.user_id=u.user_id
where u.full_name like '"+keyWord+"%'" + "and u.user_id = "+id
Note: you probably do not need the full_name
comparison in your WHERE clause, as I assume user_id is unique per user, so will get you the correct record.
The query below should be all you need:
"select u.full_name, ui.user_contact_state, ui.user_contact_city_town, ui.user_contact_country
from user u
left outer join user_contact_info ui on ui.user_id=u.user_id
where u.user_id = "+id
精彩评论