[英]MySQL: Select nested list from multiple tables
I'm working on a game where I have some images
, where each image has some keywords
and each keyword have a score. 我正在开发一个有一些
images
的游戏,其中每个图像都有一些keywords
,每个关键字都有一个分数。
I want to get all images, and it's keywords (with scores) by gameId
我想获取所有图像,它是
gameId
的关键字(带有分数)
My database structure is like this: 我的数据库结构是这样的:
Games 游戏类
Image 图片
Keywords 关键词
I want to get a JSON response that looks like this: 我想要一个看起来像这样的JSON响应:
{
gameId: 1,
images: [
{
imageId: 1,
keywords: [
{
keyword: "keyword1",
score: 1234
}...
]
}...
]
}
My SQL query looks like this: 我的SQL查询如下所示:
SELECT g.*, (SELECT i.*, kw.*
FROM image i LEFT OUTER JOIN keywords kw on kw.imageId=i.id
where i.gameId = g.id) as images
FROM games g LEFT OUTER JOIN users u on u.id=g.byUser WHERE g.id = 1
But I get this error: 但是我得到这个错误:
1241 - Operand should contain 1 column(s)
1241-操作数应包含1列
What am I doing wrong? 我究竟做错了什么? Can't I nest an array in the response?
我不能在响应中嵌套一个数组吗?
No you cannot nest an array in the select list. 不,您不能在选择列表中嵌套数组。
Maybe you need this: 也许您需要这样:
SELECT g.*, i.*, kw.*
FROM games g LEFT OUTER JOIN users u on u.id=g.byUser
LEFT OUTER JOIN image i on i.gameId = g.id
LEFT OUTER JOIN keywords kw on kw.imageId = i.id
WHERE g.id = 1;
But there will be extra g.* values in some rows 但是在某些行中会有额外的g。*值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.