从 2 个不同的数据库中选择 2 个表 (ACCESS)

发布于 2024-09-07 02:39:31 字数 960 浏览 5 评论 0原文

这是我所拥有的连接

strCon="DBQ=" & Server.Mappath("db.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};PWD=password;"
set adoCon=server.createobject("adodb.connection")
adoCon.Open strCon

,为了使用 2 个数据库,我有 2 个 adoCon,当我进行选择时,我从我

现在需要解决问题的每个数据库中进行选择... 在这种情况下,我只能从一个人那里获取所有信息,然后再从另一个人那里获取所有信息。但我想要的是能够将它们组合在一起。

db1.tblcats 有类别,db2.tblcats 有类别和子类别 因此,除了能够同时选择两者之外,我还需要能够知道什么猫来自哪个数据库,


是我的代码,这是我的代码,

strSQL = "SELECT name FROM tblcats union " _ 
& "select name from [MS Access;PWD=pass;DATABASE=" & Server.Mappath("../shop.mdb") & "].tblcats as bcats where bcats.father=50"
                                rs.CursorType = 3
                                rs.LockType = 3
                                rs.Open strSQL, strCon
while not rs.eof
response.write rs("name")&"<br>"
rs.movenext
wend

我如何知道什么记录来自哪个数据库?因为我需要对每个人采取不同的行动

here is the connection i have

strCon="DBQ=" & Server.Mappath("db.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};PWD=password;"
set adoCon=server.createobject("adodb.connection")
adoCon.Open strCon

so in order to work with the 2 databases i have 2 adoCon and when i do the select i select from each db i need

now for the problem...
in this situation i will be able only to get all the info from one and then from the other one. but what i want is to be able to put the together.

db1.tblcats has categories and db2.tblcats has categories and subcategories
so in addition to be able to select both of the together, i need to be able to know what cat is from what db


Step 2 after the big help

this is my code

strSQL = "SELECT name FROM tblcats union " _ 
& "select name from [MS Access;PWD=pass;DATABASE=" & Server.Mappath("../shop.mdb") & "].tblcats as bcats where bcats.father=50"
                                rs.CursorType = 3
                                rs.LockType = 3
                                rs.Open strSQL, strCon
while not rs.eof
response.write rs("name")&"<br>"
rs.movenext
wend

how can i know what record came from what db? cause i need to act difrently for each one

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2024-09-14 02:39:31

您可以使用 IN:

SELECT t1.*, t2.* 
FROM T1 
INNER JOIN 
(SELECT * FROM atable 
IN 'C:\Docs\DB2.mdb') t2
ON t1.ID=t2.ID

EDIT:

sc = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\docs\other.mdb"
cn.open sc

s="SELECT * FROM t1 INNER JOIN " _
& "[MS Access;PWD=databasePWD;DATABASE=C:\docs\db.mdb].t2 ON t1.ID=t2.ID"

rs.Open s, cn

EDIT 2:

您可以使用别名来标识字段来自哪个数据库:

s="SELECT * FROM table1 t INNER JOIN " _
& "[MS Access;PWD=databasePWD;DATABASE=C:\docs\db.mdb].m ON t.ID=m.ID"

 msgbox rs.fields("m.code") & " " & rs.fields("t.code")

EDIT 3

或者您可以添加虚拟字段:

 SELECT 1 AS "DB", Field, Field FROM  ...
 UNION ALL
 SELECT 2 AS "DB", Field, Field FROM

UNION ALL 通常更快。

You can use IN:

SELECT t1.*, t2.* 
FROM T1 
INNER JOIN 
(SELECT * FROM atable 
IN 'C:\Docs\DB2.mdb') t2
ON t1.ID=t2.ID

EDIT:

sc = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\docs\other.mdb"
cn.open sc

s="SELECT * FROM t1 INNER JOIN " _
& "[MS Access;PWD=databasePWD;DATABASE=C:\docs\db.mdb].t2 ON t1.ID=t2.ID"

rs.Open s, cn

EDIT 2:

You can use the aliases to identify which database a field is from:

s="SELECT * FROM table1 t INNER JOIN " _
& "[MS Access;PWD=databasePWD;DATABASE=C:\docs\db.mdb].m ON t.ID=m.ID"

 msgbox rs.fields("m.code") & " " & rs.fields("t.code")

EDIT 3

Or you can add a virtual field:

 SELECT 1 AS "DB", Field, Field FROM  ...
 UNION ALL
 SELECT 2 AS "DB", Field, Field FROM

UNION ALL is usually faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文