SQL新手求助:Chinook数据库查询语句报错排查
Fixing Your Chinook SQL Query
Hey there! Let's break down the issues in your query—these are easy mistakes to make when you're getting started with SQL, so no worries. Here's what's going wrong and how to fix it:
- Typos in column names: You wrote
Invoice.Invocieid(note the swapped "o" and "v") instead ofInvoice.Invoiceidin your JOIN condition with InvoiceLine. The database can't find that misspelled column, which is a big reason your query might fail or return garbage. - Incorrect JOIN condition: You tried joining InvoiceLine using
InvoiceLine.InvoiceLineid = Invoice.Invocieid—that's not right!InvoiceLineidis the primary key of the InvoiceLine table, not the foreign key linking to Invoice. You need to useInvoiceLine.InvoiceId = Invoice.InvoiceIdto properly connect the two tables. - Garbled alias quotes: The alias
鈥淭otal Spent鈥�uses messed-up full-width Chinese quotes that the database can't parse. Replace those with standard English double quotes like"Total Spent"(or backticks if your database prefers them) to get a valid alias. - Missing space before FROM: You didn't leave a space between your alias and the
FROMclause—databases treat this as invalid syntax because it can't distinguish the alias from the next keyword. - Ambiguous
totalreference: In your WHERE clause,totaldoesn't specify which table it comes from. Even if Invoice is the only table with a Total column here, it's good practice to writeInvoice.Totalto avoid confusion later.
Here's the corrected, formatted query that should work as expected:
SELECT DISTINCT Invoice.Invoiceid, Invoice.Customerid, Invoice.InvoiceDate, Invoice.BillingCountry, Invoice.Total AS "Total Spent" FROM Invoice JOIN Customer ON Invoice.Customerid = Customer.Customerid JOIN InvoiceLine ON InvoiceLine.InvoiceId = Invoice.InvoiceId WHERE Invoice.Total BETWEEN 10.00 AND 50.00 ORDER BY Invoice.InvoiceDate LIMIT 100;
I also formatted the query with line breaks to make it easier to read—this will help you catch typos and logic errors faster in the future.
内容的提问来源于stack exchange,提问作者Data Slayer
相关产品推荐
相关产品推荐

