我有以下表格结构,并试图从这些表格中做出报告:
___ BillableDatas
|--------|------------|----------|----------|----------|--------------|
| BIL_Id | BIL_Date | BIL_Type | BIL_Item | BIL_Rate | BIL_Quantity |
|--------|------------|----------|----------|----------|--------------|
| 1 | 2018-03-01 | Night | 0 | 105 | 1 |
| 2 | 2018-03-02 | Night | 0 | 105 | 1 |
| 3 | 2018-03-01 | Item | 30 | 15 | 2 |
| 4 | 2018-03-01 | Item | 31 | 21 | 1 |
| 5 | 2018-03-02 | Item | 30 | 15 | 2 |
| 6 | 2018-03-02 | Item | 31 | 21 | 1 |
|--------|------------|----------|----------|----------|--------------|
___ 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 |
|-----------|-----------|
___库存
|--------|-----------|----------|
| INV_Id | INV_Name | INV_Rate |
|--------|-----------|----------|
| 30 | Breakfast | 15.00 |
| 31 | Paid-TV | 21.00 |
| 32 | Wine | 19.00 |
|--------|-----------|----------|
我需要列出这样的项目:
|--------|------------|----------|----------|------------|----------|--------------|---------------------|---------------------|
| BIL_Id | BIL_Date | BIL_Type | INV_Name | BIL_RateId | BIL_Rate | BIL_Quantity | ApplicableTaxesRate | ApplicableTaxesName |
|--------|------------|----------|----------|------------|----------|--------------|---------------------|---------------------|
| 1 | 2018-03-01 | Night | NULL | 0 | 105.00 | 1 | 5.000,19.975,5.000 | Tax 1,Tax 3,Tax 2 |
| 2 | 2018-03-02 | Night | NULL | 0 | 105.00 | 1 | 19.975,5.000,5.000 | Tax 3,Tax 2,Tax 1 |
| 3 | 2018-03-01 | Item | NULL | 0 | 15.00 | 3 | 19.975,5.000 | Tax 3,Tax 2 |
| 4 | 2018-03-01 | Item | NULL | 0 | 21.00 | 1 | 0 | 0 |
| 5 | 2018-03-02 | Item | NULL | 0 | 15.00 | 2 | 5.000,19.975 | Tax 2,Tax 3 |
| 6 | 2018-03-02 | Item | NULL | 0 | 21.00 | 1 | 0 | 0 |
|--------|------------|----------|----------|------------|----------|--------------|---------------------|---------------------|
对总数的说明:
我的最后一次尝试就是这个:
SELECT BIL_Id
, BIL_Date
, BIL_Type
, BIL_RateId
, BIL_Rate
, BIL_Quantity
, GROUP_CONCAT(COALESCE(STX_Amount, "0")) AS ApplicableTaxesRate
, GROUP_CONCAT(COALESCE(STX_TaxeName, "0")) AS ApplicableTaxesName
FROM ___BillableDatas
LEFT JOIN ___Inventory
ON ___BillableDatas.BIL_Item=___Inventory.INV_Id
LEFT JOIN ___SalesTaxes
ON FIND_IN_SET(STX_Id, BIL_ApplicableTaxes) > 0
GROUP BY BIL_Id
ORDER BY BIL_Date ASC
如果需要,请参阅此SQLFiddle以帮助您:
http://sqlfiddle.com/#!9/ebc997
感谢。
答案 0 :(得分:0)
作为答案原因发布评论不会格式化。
SELECT BIL_Id
, BIL_Date
, BIL_Type
, BIL_RateId
, BIL_Rate
, BIL_Quantity
, GROUP_CONCAT(COALESCE(STX_Amount, "0")) AS ApplicableTaxesRate
, GROUP_CONCAT(COALESCE(STX_TaxeName, "0")) AS ApplicableTaxesName
FROM ___BillableDatas BD
LEFT JOIN ___ApplicableTaxesInventory AI
ON FIND_IN_SET(AI.ATX_INVId, BD.BIL_Item)
LEFT OUTER JOIN ___SalesTaxes ST
ON AI.ATX_STXId = ST.STX_Id
GROUP BY BIL_Id
ORDER BY BIL_Date ASC
此查询似乎提供了您期望的结果,但数据不匹配,因为样本数据与问题不匹配。如果这不是您所期望的,请告诉我。