"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;
从我的用户u表中我想要全名,从我的user_contact_info表ui中我想要 州城市和国家。
我是否正在编写连接查询来根据 id 获取所有值。我想知道我是否 正确或不正确地编写查询。如果写错了应该怎么写。请帮我编写联接查询。
另外,在结果集中,如果我使用单个表,如何获取值 我将我的 rs 写为
if(rs.next()){
User user = new User();
user.setFullname(rs.get(full_name);
}
如果使用第二个连接表,如何获取和设置我的对象中的值。
最佳答案
"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
注意:您可能不需要 WHERE 子句中的 full_name
比较,因为我假设每个用户的 user_id 是唯一的,因此将为您提供正确的记录。
下面的查询应该是您所需要的:
"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
关于java - 这个mysql查询是对还是错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4126144/