0

我有两张桌子,网络和邮件。目前我正在使用此查询从两个表中获取数据。

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
4

3 回答 3

3
try this 

select * , 'web' as tablename from web where name='abc' union
select * ,'mail' as table namefrom mail where name='abc'
于 2013-10-07T09:46:10.927 回答
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();
}
于 2013-10-07T09:52:19.430 回答
0
select * , 'web' as source from web where name='abc' union
select *, 'mail' as source from mail where name ='abc'
于 2013-10-07T09:48:17.517 回答