使用mySQL

时间:2018-03-24 18:38:23

标签: mysql

我有以下表格结构:

___库存

|--------|------------|----------|
| INV_Id | INV_Name   | INV_Rate |
|--------|------------|----------|
|     30 |  Breakfast |    15.00 |
|     31 |       Bike |    21.00 |
|     32 |      Visit |    19.00 |
|--------|------------|----------|

___ SalesTaxes

|--------|--------------|------------|
| STX_Id | STX_TaxeName | STX_Amount |
|--------|--------------|------------|
|      8 |        Tax 1 |      5.000 |
|      9 |        Tax 2 |      5.000 |
|     10 |        Tax 3 |     19.975 |
|--------|--------------|------------|

STX_Amount是一个百分比。

___ ApplicableTaxesInventory

|-----------|-----------|
| ATX_INVId | ATX_STXId |
|-----------|-----------|
|        30 |         9 |
|        30 |        10 |
|        31 |         9 |
|-----------|-----------|
  • ATX_INVId是具有___库存的物品ID链接。
  • ATX_STXId是与___ SalesTaxes的税号ID链接。

我需要列出___Inventory中的项目并获取这些信息

这样的方法就是这样:

|--------|----------|----------|-----------|
| INV_Id | INV_Rate | Tax_List | sum_taxes |
|--------|----------|----------|-----------|
|     30 |    15.00 |     9,10 |    24.975 |
|     31 |    21.00 |        9 |     5.000 |
|     32 |    19.00 |          |           |
|--------|----------|----------|-----------|

我的最后一次尝试就是这个:

SELECT *, 
(SELECT SUM(st.STX_Amount) 
FROM ___SalesTaxes st
WHERE FIND_IN_SET(st.STX_Id, ati.ATX_INVId) > 0) as sum_taxes
FROM  ___Inventory i 
INNER JOIN ___ApplicableTaxesInventory ati
    ON ___Inventory.INV_Id = ___ApplicableTaxesInventory.ATX_INVId
ORDER BY INV_Name ASC

如果需要,请参阅此SQLFiddle以帮助您:
http://sqlfiddle.com/#!9/622ea0/2

感谢。

2 个答案:

答案 0 :(得分:0)

在你的SQL小提琴中,你给表别名,但是忘了使用你引用它们的别名:

SELECT *, 
(SELECT SUM(st.STX_Amount) 
FROM ___SalesTaxes st
WHERE FIND_IN_SET(st.STX_Id, ati.ATX_INVId) > 0) as sum_taxes
FROM  ___Inventory i 
INNER JOIN ___ApplicableTaxesInventory ati
    ON i.INV_Id = ati.ATX_INVId
ORDER BY INV_Name ASC

获得所需内容的更好方法是将表格加在一起:

SELECT
  INV_Id,
  INV_Name,
  GROUP_CONCAT(STX_Id) as `Tax_List`,
  INV_Rate,
  SUM(STX_Amount) as sum_taxes
FROM ___Inventory i
JOIN ___ApplicableTaxesInventory ati
ON ati.ATX_INVId = i.INV_Id
JOIN ___SalesTaxes st
ON st.STX_Id = ati.ATX_STXId
GROUP BY INV_Id

<强> Results

| INV_Id |    INV_Name | INV_Rate | sum_taxes |
|--------|-------------|----------|-----------|
|     30 |   Breakfast |       15 |    24.975 |
|     31 | Bike rental |       21 |         5 |

答案 1 :(得分:0)

您需要使用LEFT JOINs来确保即使没有适用的税款,您也可以获得所有库存商品的行数。然后,有几个IFNULLsNULL值更改为更有用的内容(除非您对NULL感到满意):

SELECT
  INV_Id,
  INV_Name,
  IFNULL(GROUP_CONCAT(STX_Id), "") as `Tax_List`,
  INV_Rate,
  IFNULL(SUM(STX_Amount), 0)  as sum_taxes
FROM ___Inventory i
LEFT JOIN ___ApplicableTaxesInventory ati
ON ati.ATX_INVId = i.INV_Id
LEFT JOIN ___SalesTaxes st
ON st.STX_Id = ati.ATX_STXId
GROUP BY INV_Id

输出:

INV_Id  INV_Name    Tax_List    INV_Rate    sum_taxes
30      Breakfast   10,9        15          24.975
31      Bike rental 9           21          5
32      Visit                   19          0