這個問題讓我一頭霧水。我嘗試在 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' );