【问题标题】:Query for extracting info from one table based on other in strongloop查询基于strongloop中的另一个表从一个表中提取信息
【发布时间】:2016-03-10 08:46:02
【问题描述】:

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

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

例子:

表名:帖子

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.

模型关系:post.json

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

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

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

和//这里我也只是检查了postname,但不需要这个问题。

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

}

【问题讨论】:

    标签: javascript mysql node.js loopbackjs strongloop


    【解决方案1】:

    如果跳过“正式”关系而只使用操作挂钩 (https://docs.strongloop.com/display/public/LB/Operation+hooks) 以便在进行查找时可以操纵结果怎么样?

    【讨论】:

    • 你的意思是分别调用 post 和 share ,最后连接两个数据?
    • 我的意思是——当一个查找操作被调用时,理论上你可以将共享的帖子附加到它上面。
    猜你喜欢
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    相关资源
    最近更新 更多