简体   繁体   English

在Express API上联接两个表

[英]Join two tables on express api

I'm building an api with Express.js. 我正在用Express.js构建一个api。 Right now my very first testes are working. 现在,我的第一个睾丸正在工作。 Now, i would like to get values from two different tables. 现在,我想从两个不同的表中获取值。 Per example, let's say i have the next 2 tables 举例来说,假设我有接下来的2张桌子

Table_A
Id: 1,
Name: Chuck,
Age: 30

Table_B

Id: 1,
NickName: Chuckthulhu,
DateStart: 2018-11-01,
IdTableA: 1

And what i expect to return on my api is: 我希望在api上返回的内容是:

{Id: 1, Name: "Chuck", NickName: "Chucthulhu", DateStart: "2018-11-01"}

How can i do that? 我怎样才能做到这一点? This my code so far: 到目前为止,这是我的代码:

    var express = require("express");
var bodyParser = require("body-parser");
var sql = require("mssql");
var app = express();

app.use(bodyParser.json());

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
    next();
});

 var server = app.listen(process.env.PORT || 8080, function () {
    var port = server.address().port;
    console.log("App now running on port", port);
 });

 var dbConfig = {
     user:  "theUser",
     password: "thePassword",
     server: "theServer",
     database: "theDb"
 };

var  executeQuery = function(res, query){
     sql.connect(dbConfig, function (err) {
         if (err) {
                     console.log(err);
                     res.send(err);
                  }
                  else {
                         // create Request object
                         var request = new sql.Request();
                         // query to the database
                         request.query(query, function (err, result) {
                           if (err) {
                                      console.log(err);
                                      res.send(err);
                                     }
                                     else {
                                       res.send(result);
                                            }
                               });
                       }
      });
}

//GET API
app.get("/api/MyApi", function(req, res){
                var query = "select * from [Table_A]";
                executeQuery (res, query);
});

I'm very new on this. 我对此很陌生。 As i see, it's a simple query to get the info... if that's the logic, i just need to do a join or left join to the Table_B? 依我之见,这是一个简单的查询来获取信息...如果是这样的逻辑,我只是需要做一个joinleft join到表-B?

I'm using Javascript, Node and Express.js, and the DB it's on SQL Server Thanx in advance. 我正在使用Javascript,Node和Express.js,并且数据库预先在SQL Server Thanx上。

--From your example, IdTableA is the foreign key in Table B which relates it to a record
--in Table A, you would want to Inner Join on that to link it to Table A
--Alias your table''s with a and b and then select the appropriate columns from each

select a.[Id], a.[Name], b.[NickName], b.[DateStart] 
from [Table_A] AS a
INNER JOIN [Table_B] AS b
ON b.[IdTableA] = a.[Id]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM