简体   繁体   English

内部连接中的mysql语法错误

[英]mysql syntax error in inner join

I am trying to perform a query with inner join and adding a where but I get the following error: 我正在尝试通过内部联接执行查询,并添加一个位置,但出现以下错误:

1064 - You have an error in your SQL syntax; 1064-您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where orders.user_id = 31) 查看与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'where order.user_id = 31)附近使用

And the query is structured in this way: 查询的结构是这样的:

SELECT
    orders.id,
    order_details.order_id,
    orders.user_id,
    orders.numero_orden,
    order_details.part_id,
    order_details.cantidad
FROM
((orders
    INNER JOIN order_details
        ON orders.id = order_details.order_id
    where orders.user_id = 31) 
INNER JOIN parts
    ON order_details.part_id  = parts.id);

Your query looks like you are trying to use MS Access syntax. 您的查询看起来像您正在尝试使用MS Access语法。 In MySQL, you don't need to wrap components of a multiple join with parentheses. 在MySQL中,您不需要用括号包装多重联接的组件。 Consider this version: 考虑以下版本:

SELECT
    o.id,
    od.order_id,
    o.user_id,
    o.numero_orden,
    od.part_id,
    od.cantidad
FROM orders o
INNER JOIN order_details od
    ON o.id = od.order_id
INNER JOIN parts p
    ON od.part_id  = p.id
WHERE
    o.user_id = 31;

Notes: I refactored your query to use table aliases, leaving it much easier to read. 注意:我将查询重构为使用表别名,从而使其更易于阅读。 Also, I don't really see the point of joining to the parts table, because you never select anything from it. 另外,我真的看不到连接到parts表的意义,因为您永远不会从中选择任何内容。 Perhaps you intended to do that, but it did not make it into your question. 也许您打算这样做,但是并没有将其纳入您的问题。

You can't put the WHERE clause there, it needs to be at the end, or you could try leaving it where it is and replacing the WHERE with an AND to make it part of the join clause. 您不能在此处放置WHERE子句,它必须放在最后,或者您可以尝试将其保留在原处,并用AND替换WHERE,以使其成为join子句的一部分。 I think the result will be the same. 我认为结果将是相同的。

So either: 所以:

SELECT
    orders.id,
    order_details.order_id,
    orders.user_id,
    orders.numero_orden,
    order_details.part_id,
    order_details.cantidad
FROM
    orders
    INNER JOIN order_details
        ON orders.id = order_details.order_id
    INNER JOIN parts
        ON order_details.part_id  = parts.id
WHERE 
    orders.user_id = 31;

or 要么

SELECT
    orders.id,
    order_details.order_id,
    orders.user_id,
    orders.numero_orden,
    order_details.part_id,
    order_details.cantidad
FROM
    orders
    INNER JOIN order_details
        ON orders.id = order_details.order_id
           AND orders.user_id = 31
    INNER JOIN parts
        ON order_details.part_id  = parts.id;

Not sure you really need those brackets round the joins either, so I removed them. 不确定您是否真的需要在连接处使用这些括号,因此我删除了它们。 I would recommend that maybe you should study the allowed syntax for a SQL query in more detail to understand about where you need to place the different parts. 我建议也许您应该更详细地研究SQL查询的允许语法,以了解需要在何处放置不同部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM