【发布时间】:2020-10-08 10:05:08
【问题描述】:
如何更正 Node.js mysql 中的子选择查询?
例子:
TABLE1
|ID: (AI) |title: | content: | image_id |
|1 | some | something| 1 |
TABLE 2
|ID: (AI) |title: | src: |
|1 | any | base64...|
我想要什么结果:
select_result = [
{
title: some,
content: something,
image: {
title: any,
src: 'base64..'
},
}
]
我尝试了什么:
SELECT title, content, (SELECT * FROM TABLE2 WHERE id = image_id) as 'image' from TABLE1;
错误:
code: 'ER_OPERAND_COLUMNS',
errno: 1241,
sqlMessage: 'Operand should contain 1 column(s)',
此子查询正在运行,但我需要其他结构:
SELECT title, content, (SELECT src FROM TABLE2 WHERE id = image_id) as 'image' from TABLE1;
【问题讨论】:
-
查询的
SELECT部分中使用的子查询必须返回单列,而不是多列,如错误所示。 -
我怎样才能得到这个对象?
-
一个 mysql 结果总是一个平面数组。如果你想要多维的东西,我建议你在你的应用程序代码中这样做。
标签: mysql node-mysql