使用MySQL从不同表中查询列表项(含表结构示例)
Got it, let's walk through how to query data from your two tables. First, let's restate your table structures clearly for reference:
Inventory Table
| INV_Id | INV_Name | INV_Rate |
|---|---|---|
| 30 | Breakfast | 15.00 |
| 31 | Bike | 21.00 |
| 32 | Visit | 19.00 |
SalesTaxes Table
| STX_Id | STX_TaxeName | STX_Amount |
|---|---|---|
| 8 | Tax 1 | 5.000 |
| 9 | Tax 2 | 5.000 |
| 10 | Tax 3 | 19.975 |
Now let's cover common query scenarios you might need:
1. Fetch all inventory items
If you just need to pull every entry from the Inventory table with all its details, use this straightforward SELECT statement:
SELECT INV_Id, INV_Name, INV_Rate FROM Inventory;
This will return all rows exactly as they appear in your Inventory table structure.
2. Fetch all sales tax entries
Similarly, to get every tax record from the SalesTaxes table:
SELECT STX_Id, STX_TaxeName, STX_Amount FROM SalesTaxes;
3. Combine data from both tables (assuming a relationship)
If your inventory items link to specific taxes (even though you didn't include a foreign key in the given structure), let's say there's an INV_STX_Id column in Inventory that maps to STX_Id in SalesTaxes. You can join the tables to get combined data like this:
SELECT i.INV_Id, i.INV_Name, i.INV_Rate, s.STX_TaxeName, s.STX_Amount FROM Inventory i JOIN SalesTaxes s ON i.INV_STX_Id = s.STX_Id;
If you want to include inventory items that don't have an associated tax (and show NULL for tax fields), swap JOIN with LEFT JOIN.
4. Filter specific items
For example, to get only inventory items with a rate higher than 18:
SELECT INV_Name, INV_Rate FROM Inventory WHERE INV_Rate > 18.00;
Or to get taxes with an amount greater than 5:
SELECT STX_TaxeName, STX_Amount FROM SalesTaxes WHERE STX_Amount > 5.000;
If you had a specific combined query in mind—like calculating total cost including tax for inventory items—just let me know and I can tweak this further!
内容的提问来源于stack exchange,提问作者user9516731

