我和我有两张桌子,网络和邮件。目前我正在使用此查询从两个表中获取数据。
PreparedStatement ps = con.prepareStatement("(select * from web where name='abc') union (select * from mail where name='abc')");
ResultSet rs = ps.executeQuery();
while(rs.next()){
bw.write(rs3.getString("name")+"~"+rs3.getString("age")+"~"+rs3.getString("profession");
bw.newLine();
}
输出是这样的。
+------+------+------------+
| name | age | profession |
+------+------+------------+
| abc | 20 | doctor |
| abc | 20 | engineer |
+------+------+------------+
在文件中就像这样
abc~20~doctor
abc~20~engineer
但是如何在结果集中添加额外的列,这将为我提供此格式的数据
abc~20~doctor~web
abc~20~engineer~mail
答案 0 :(得分:3)
try this
select * , 'web' as tablename from web where name='abc' union
select * ,'mail' as table namefrom mail where name='abc'
答案 1 :(得分:1)
试试这个
PreparedStatement ps = con.prepareStatement("(select name,age,profession ,'web' AS ExtraColumnfrom from web where name='abc') union (select name,age,profession ,'mail' AS ExtraColumnfrom mail where name='abc')");
ResultSet rs = ps.executeQuery();
while(rs.next()){
bw.write(rs3.getString("name")+"~"+rs3.getString("age")+"~"+rs3.getString("profession")+"~"+rs3.getString("ExtraColumnfrom ");
bw.newLine();
}
答案 2 :(得分:0)
select * , 'web' as source from web where name='abc' union
select *, 'mail' as source from mail where name ='abc'