SQL-将联接表存储为键:对象

时间:2018-08-31 15:04:35

标签: sql sql-server angular express

我有2个问题:

  1. 检索联接表时是否无法检索重复的ID?

  2. 是否可以将Join table中的所有内容放在一个键下?如果是,我们可以保留id的重复,因为它应该在另一个对象下。

我从快递服务器上运行此SQL

var query = `BEGIN TRAN
    UPDATE ${accTable} SET [token]='${hash}' WHERE [accountID]=${id};

    SELECT a.accountID AS accID, a.*, ai.*
    FROM ${accTable} AS a
    INNER JOIN ${accInfoTable} as ai
        ON a.accountID = ai.accountID
    WHERE a.accountID=${id}
    COMMIT`; 

我得到的是一个将所有内容结合在一起的json

{
  "accID": 1,
// 1. is it possible to only get 1 of the accountID's?
  "accountID": [
    1,
    1
  ],
  "username": "john",
  "password": "hash",
  "hasTPD": false,
  "isConfigured": false,
  "token": "jwtToken",
// 2. everything underneath should be under one key
  "accInfoID": 3,
  "firstName": "John",
  "surname": "Doe",
  "email": "mail@gmail.com",
  "phone": 0400111222,
  "securityLevel": 99,
  "securityID": 1,
  "roleID": 3,
  "managerID": 1,
  "profilePicID": 1
}

这是我想要实现的:

{
  "accID": 1,
  "accountID": 1         // 1. no duplicate keys
  "username": "john",
  "password": "hash",
  "hasTPD": false,
  "isConfigured": false,
  "token": "jwtToken",
  "accountInfo": {       // 2. everything from accountInfoTable is under 1 item
      "accInfoID": 3,
      "firstName": "John",
      "surname": "Doe",
      "email": "mail@gmail.com",
      "phone": 0400111222,
      "securityLevel": 99,
      "securityID": 1,
      "roleID": 3,
      "managerID": 1,
      "profilePicID": 1,
      "accountID": 1     // 2. the duplicate comes in here (as it was retrieved from here in the first place)

  }
}

谢谢:)

0 个答案:

没有答案