查询在强循环中基于一个表从另一个表中提取信息

Query for extracting info from one table based on other in strongloop

本文关键字:提取 信息 另一个 循环 查询 于一个      更新时间:2023-09-26

我有两个表帖子和分享,帖子有很多分享。 我想使用 userId(由所有者用户发布)获取帖子表中的所有数据,并使用相同的用户 ID 检查共享表,即如果某个人将帖子共享给其他用户,如果任何条件为真,我需要获取数据。

如果由所有者发布或由共享表中的其他用户共享,我想在帖子表中获取数据。

例:

表名称:帖子

id(pk) postname userid
    1  abc       10
    2  xxx       10
    3  yyy       11
    4  zzz       12
    5  bbb       13 

表名称:共享

id postid(fk) userid
1   3           10
2   4           10
3   3           11
4   1           12

预期输出:按用户 ID 10 查找的示例

 id postname userid
  1  abc       10  // this record created by user 10 (owner)
  2  xxx       10  // this record created by user 10 (owner)
  3  yyy       11  // this record shared by other user to user 10.
  4  zzz       12  // this record shared by other user to user 10.

模型关系:后.json

 "relations": {
    "shares": {
      "type": "hasMany",
      "model": "share",
      "foreignKey": "postid"
    }
  }

我尝试以下查询,但出现错误

{
  "where": {
    "or": [
      {
        "userid": 10
      },
      {
        "include": {
          "relation": "shares",
          "scope": {
            "where": {
              "userid": 10
            }
          }
        }
      }
    ]
  }
}

和//在这里我也只是检查帖子名称,但不需要这个问题。

{
  "where": {
    "or": [
      {
        "userid": 10
      },
      {
        "postname": "xxx"
      }
    ],
    "include": {
      "relation": "shares",
      "scope": {
        "where": {
          "userid": 10
        }
      }
    }
  }
}

跳过"正式"关系,只使用操作钩子(https://docs.strongloop.com/display/public/LB/Operation+hooks),以便在执行查找时可以操作结果怎么样?