日期:2014-05-20  浏览次数:20657 次

java这样的查询语句能写吗?
有两个表都有name这个字段,值是不一样的,能一句SQL查询出来吗
Java code

select name from table1 where orderID = '123'
select name from table2 where  age = '35'
这两条语句能合成一条吗?如果能合并,怎么在rs里面分辨哪个是表1的结果哪个是表2的结果



------解决方案--------------------
SQL code
SELECT name, 'table1' AS category FROM table1 WHERE ...
UNION ALL
SELECT name, 'table2' AS category FROM table2 WHERE ...

------解决方案--------------------
SQL code

select name,'表1' as tempColumn from table1 where orderID = '123'
union all
select age,'表2' as tempColumn from table2 where age = '35'