为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

心有法竹 2022-07-01 07:07:57
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个表的内容,如果一个表这样实现没有问题该怎么办啊
查看完整描述

2 回答

?
MM们

TA贡献1886条经验 获得超2个赞

如果两个表的结构是相同的,可以建个临时表
==================================
补充:

表t_test1(id, age )
表中数据 1, 1
2, 2
3, 3
4, 4

表t_test2(id, age )
表中数据 1, 6
2, 7
3, 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_test1
insert into tempTest(id,age,tbl) select *,2 from t_test2

select * from tempTest

drop table tempTest
--------sql-----end-----------------

select * from tempTest查询的结果是

tid id age tbl(表的标识)
1 1 1 1
2 2 2 1
3 3 3 1
4 4 4 1
5 1 6 2
6 2 7 2
7 3 8 2

==============================================
如果你能保证两个表的主键id不重复,即
表t_test2(id, age )
表中数据改为 6, 6
7, 7
8, 8

则可以如下简化:

--------sql---start-------------------
create table test (
id int NOT NULL ,
age int ,
)

insert into test(id,age) select * from t_test1
insert into test(id,age) select * from t_test2

select * from test
drop table test
--------sql-----end-----------------
select * from test查询的结果是

id age
1 1
2 2
3 3
4 4
6 6
7 7
8 8


查看完整回答
反对 回复 2022-07-05
?
HUWWW

TA贡献1874条经验 获得超12个赞

我看到你只是要从两张表里求有符合条件的多少条, 再求和, 所以就这样写了.
select ((select count(*) from clientdefine where extra2='1')+(select count(*) from channel where extra2='1')) from dual

查看完整回答
反对 回复 2022-07-05
  • 2 回答
  • 0 关注
  • 570 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号