MYSQL-选择并将多个表连接到一个JSON

问题描述 投票:0回答:2

您能告诉我如何解决此查询吗?我有3个数据库表。

  1. 网络。
  2. 产品。
  3. 评论。

在添加“ comments”表(按预期方式工作之前,查询看起来像这样:]

SELECT networks.*, products.product, products.type 
FROM products 
JOIN networks ON products.id=networks.product_Id

现在我尝试使用附加表对其进行修改,但是它不起作用:

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

如何解决此查询?

感谢帮手:)

mysql sql join select multiple-tables
2个回答
0
投票

我认为这是您要使用的语法:

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 ...

请注意,使用表别名会缩短查询的时间,并使读和写更加容易。


0
投票

您加入了条件为[[products.id = networks.product_Id的networks表但是,对于注释表,您指定了条件[[comments.id = networks.comment_Id,但您忘记了加入comments表。

尝试 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 JOIN comments ON comments.id=networks.comment_Id
© www.soinside.com 2019 - 2024. All rights reserved.