这个问题让我一头雾水。我尝试在 mysql 中使用 Join 和子查询,但不断收到语法错误。
有问题的陈述是
SELECT Customer.customer_id, Customer.name, Order.address FROM Customer WHERE customer_id = (SELECT customer_id FROM Order WHERE customer_id = "625060836f7496e9fce3bbc6") INNER JOIN Order ON Customer.customer_id=Order.customer_id;
我尝试只使用不带子查询的查询,它工作得很好。
SELECT Customer.customer_id, Customer.name, Order.address FROM Customer INNER JOIN Order ON Customer.customer_id=Order.customer_id;
删除连接但保留子查询也可以。
SELECT Customer.customer_id, Customer.name, Order.address FROM Customer WHERE customer_id = (SELECT customer_id FROM Order WHERE customer_id = "625060836f7496e9fce3bbc6")
仅同时使用子查询和联接会导致语法错误
我似乎找不到错误。
我在这里做错了什么。
提前致谢
P粉8940084902024-04-05 00:17:29
秘密是正确的语法!
当查询多个表时,最好使用别名来引用它们,并且在多个表共享相同列名的情况下,字符串文字也应该用单引号分隔。
在这个具体示例中,子查询是多余的,只需在 where 子句中直接使用字符串文字即可。
SELECT c.customer_id, c.name, o.address FROM Customer c JOIN Order o ON c.customer_id = o.customer_id WHERE c.customer_id = ( SELECT customer_id FROM Order WHERE customer_id = '625060836f7496e9fce3bbc6' );