2017-04-21 129 views
-2

我問這個:如何在MySQL的FROM子句中使用子查詢?

Use a subquery in the FROM clause to only retrieve invoices from chamber 'H' and the invoice amount of larger than 10000 and join the result with the voyages table using the number column. Project to only retrieve the boatname and the invoice amount of the join result. Order by invoice amount.

所以,我做了這個:

SELECT chamber = 
     (
      SELECT  chamber 
      FROM  invoices 
      INNER JOIN voyages ON chambers.chamber ='H' 
           AND chambers.invoice > 10000 
           AND invoice.number=voyages.number 
     ), 
     boatname, 
     invoice 
FROM chambers, 
     voyages, 
     invoices 
WHERE chambers.chamber = invoices.chamber, 
     invoices.number = voyages.number 

這是室表:

# chamber name 
1  A  New York 

這是發票表:

# number invoice chamber 
1 8300 9189 A 

然而,它不斷給我這個錯誤:Query failed: near ",": syntax error

+3

你不能用','來分隔WHERE子句語句。你需要使用'AND'或'OR'。 – Siyual

+1

您還應該停止使用隱式的'JOIN'語法。你應該在FROM子句中永遠不要有''''。這種古老的'JOIN'風格已被棄用*超過25年*。 – Siyual

+0

@CSharper他們不會。但你應該問OP,而不是我。 – Siyual

回答

1

你的說明書說把子查詢在FROM子句中,你有它作爲一個分配腔列。你想要做的是:

SELECT b.boatname 
     ,a.invoice 
FROM (SELECT number, invoice 
     FROM invoices 
     WHERE chamber = 'H' 
      AND invoice > 10000) a 
INNER JOIN voyages b ON a.number = b.number 
ORDER BY a.invoice DESC 
+0

哈哈,太簡單了!我正在推翻它。謝謝,我現在遇到了這個問題。 – Siyah

相關問題