[英]How to join two SQL tables using javascript and ADO in Internet Explorer
I'm using Javascript and Internet Explorer's ActiveXObjects to connect to a SQL database. 我正在使用Javascript和Internet Explorer的ActiveXObjects连接到SQL数据库。 I can read out results from a single table without a problem.
我可以从单个表中读出结果,而不会出现问题。 Unfortunately, I can't figure out the syntax to join two tables that are in the same database.
不幸的是,我无法弄清楚连接同一数据库中两个表的语法。 Here's what I'm trying:
这是我正在尝试的:
//create object
var conn = new ActiveXObject("ADODB.Connection");
//store credentials
var connectionstring = "Provider=sqloledb; Data Source={omitted}; Initial Catalog={omitted}; User ID={omitted};Password={omitted}";
//open the db connection with credentials
conn.Open(connectionstring);
//create a new record set
var rs = new ActiveXObject("ADODB.Recordset");
//search the record set with SQL call
rs.Open("SELECT * FROM [order] INNER JOIN OrderItems ON (order.Id = OrderItems.Id)", conn);
If I delete "INNER JOIN" and everything after it, I'm fine. 如果删除“ INNER JOIN”及其后的所有内容,那很好。 Note that I had to put brackets around the first table's name ("order") because I think it's a protected keyword... Anyone know the right syntax for this sort of thing in Javascript?
请注意,我必须在第一个表的名称(“顺序”)前加上方括号,因为我认为这是一个受保护的关键字...任何人都知道Java语言中此类内容的正确语法吗?
This is probably the SQL syntax you are looking for: 这可能是您要查找的SQL语法:
SELECT Orders.Id
FROM [Orders]
INNER JOIN OrderItems ON [Orders].Id = OrderItems.Id
Note that the (
and )
is unnecessary. 请注意,
(
和)
是不必要的。
If you want to use aliases, then you must use those aliases in the rest of the JOIN
clause: 如果要使用别名,则必须在
JOIN
子句的其余部分中使用这些别名:
SELECT o.Id
FROM [Orders] o
INNER JOIN OrderItems oi ON o.Id = oi.Id
Once you have the SQL nailed down, you should be able to pass it into the Open
method: 一旦确定了SQL,就应该可以将其传递给
Open
方法:
var sql =
'SELECT Orders.Id ' +
'FROM [Orders] ' +
'INNER JOIN OrderItems ON [Orders].Id = OrderItems.Id';
rs.Open(sql, conn);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.