【发布时间】: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