MYSQL- select 并将多个表合并为一个 JSON
MYSQL- select and join multiple tables into one JSON
你能告诉我如何解决这个问题吗?
我有 3 个数据库表。
- 网络。
- 产品。
- 评论。
在我添加 "comments" table 之前查询看起来像这样(按预期工作):
SELECT networks.*, products.product, products.type
FROM products
JOIN networks ON products.id=networks.product_Id
现在我尝试用额外的 table 修改它,但它不起作用:
SELECT
networks.*,
products.product,
products.type,
comments.id AS comment_Id,
comments.newLine,
comments.lineComment AS comment,
comments.topComment,
comments.bottomComment
JOIN networks ON products.id=networks.product_Id,
ON comments.id=networks.comment_Id
我该如何修正这个查询?
感谢各位帮手:)
我认为这就是您所追求的语法:
SELECT
n.*,
p.product,
p.type,
c.id AS comment_id,
c.newLine,
c.lineComment AS comment,
c.topComment,
c.bottomComment
FROM products p
INNER JOIN networks n ON p.id = n.product_id
INNER JOIN comments c ON c.id = n.comment_id
即:多重连接的语法是FROM ... JOIN ... ON ... JOIN ... ON ...
。
请注意,使用 table 别名可缩短查询并使其更易于读写。
您加入了 网络 table,条件是 products.id=networks.product_Id
但是对于评论table你指定了条件comments.id=networks.comment_Id但是你忘记加入评论 table.
尝试
SELECT networks.*,
products.product,
products.type,
comments.id AS comment_Id,
comments.newLine,
comments.lineComment AS comment,
comments.topComment,
comments.bottomComment
FROM products
JOIN networks ON products.id=networks.product_Id
JOIN comments ON comments.id=networks.comment_Id
你能告诉我如何解决这个问题吗? 我有 3 个数据库表。
- 网络。
- 产品。
- 评论。
在我添加 "comments" table 之前查询看起来像这样(按预期工作):
SELECT networks.*, products.product, products.type
FROM products
JOIN networks ON products.id=networks.product_Id
现在我尝试用额外的 table 修改它,但它不起作用:
SELECT
networks.*,
products.product,
products.type,
comments.id AS comment_Id,
comments.newLine,
comments.lineComment AS comment,
comments.topComment,
comments.bottomComment
JOIN networks ON products.id=networks.product_Id,
ON comments.id=networks.comment_Id
我该如何修正这个查询?
感谢各位帮手:)
我认为这就是您所追求的语法:
SELECT
n.*,
p.product,
p.type,
c.id AS comment_id,
c.newLine,
c.lineComment AS comment,
c.topComment,
c.bottomComment
FROM products p
INNER JOIN networks n ON p.id = n.product_id
INNER JOIN comments c ON c.id = n.comment_id
即:多重连接的语法是FROM ... JOIN ... ON ... JOIN ... ON ...
。
请注意,使用 table 别名可缩短查询并使其更易于读写。
您加入了 网络 table,条件是 products.id=networks.product_Id 但是对于评论table你指定了条件comments.id=networks.comment_Id但是你忘记加入评论 table.
尝试
SELECT networks.*,
products.product,
products.type,
comments.id AS comment_Id,
comments.newLine,
comments.lineComment AS comment,
comments.topComment,
comments.bottomComment
FROM products
JOIN networks ON products.id=networks.product_Id
JOIN comments ON comments.id=networks.comment_Id