请教下怎么能把2个SQL语句合并成一个?

String strSql = "select count(*) from clientdefine where extra2='1' ";
String sql="select count(*) from channel where extra2='1'";

Database db = DatabaseTools.GetDatabase();
return db.ExecuteQueryInt(strSql)+db.ExecuteQueryInt(sql);
怎么能把2个SQL语句合并成一个
返回结果我想用一个,例如 这样返回return db.ExecuteQueryInt(SQL)
请往下看
String sql = "select clientname,ip,port,extra1,extra2,id from clientdefine where extra2='1' limit "+start+","+end;【这里我想实现的是分页】
System.out.println(sql);
Vector vector = db.ExecuteQueryVector(sql); Systems.out.println(vector.size())【打印的是5】

sql = "select channel,ip,port,extra1,extra2,id from channel where extra2='1'";
Vector vector1 = db.ExecuteQueryVector(sql); Systems.out.println(vector1.size())【打印的是4】

vector.addAll(vector1);
if(vector.size()==0){
out.write("<tr><td colspan=6 align=center>(没有相关记录)</td></tr>");
}else{
for(int i=0;i<vector.size();i++){
Systems.out.println(vector.size())【打印的是9】
}
现在是我实现分页的时候出现问题了
select clientname,ip,port,extra1,extra2,id from clientdefine where extra2='1' limit 0,5
因为用到2个表的内容,如果一个表这样实现没有问题
该怎么办啊

心有法竹
浏览 571回答 2
2回答

MM们

如果两个表的结构是相同的,可以建个临时表==================================补充:表t_test1(id, age )表中数据 1, 12, 23, 34, 4表t_test2(id, age )表中数据 1, 62, 73, 8如想把俩表合并可以如下:--------sql---start-------------------create table tempTest (tid int primary key IDENTITY (1, 1) NOT NULL,id int ,age int ,tbl int)insert into tempTest(id,age,tbl) select *,1 from t_test1insert into tempTest(id,age,tbl) select *,2 from t_test2select * from tempTestdrop table tempTest--------sql-----end-----------------select * from tempTest查询的结果是tid id age tbl(表的标识)1 1 1 12 2 2 13 3 3 14 4 4 15 1 6 26 2 7 27 3 8 2==============================================如果你能保证两个表的主键id不重复,即表t_test2(id, age )表中数据改为 6, 67, 78, 8则可以如下简化:--------sql---start-------------------create table test (id int NOT NULL ,age int ,)insert into test(id,age) select * from t_test1insert into test(id,age) select * from t_test2select * from testdrop table test--------sql-----end-----------------select * from test查询的结果是id age1 12 23 34 46 67 78 8

HUWWW

我看到你只是要从两张表里求有符合条件的多少条, 再求和, 所以就这样写了.select ((select count(*) from clientdefine where extra2='1')+(select count(*) from channel where extra2='1')) from dual
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

SQL Server