我有以下表格结构,并试图从这些表格中做出报告:
___库存
|--------|----------|
| INV_Id | INV_Name |
|--------|----------|
| 34 | Popcorn |
| 35 | Movie |
|--------|----------|
___ SalesTaxes
|--------|--------------|------------|
| STX_Id | STX_TaxeName | STX_Amount |
|--------|--------------|------------|
| 8 | Tax 1 | 5.000 |
| 9 | Tax 2 | 5.000 |
|--------|--------------|------------|
STX_Amount是一个百分比。
___ ApplicableTaxesInventory
|-----------|-----------|
| ATX_INVId | ATX_STXId |
|-----------|-----------|
| 34 | 8 |
| 34 | 9 |
|-----------|-----------|
我需要列出项目和适用的税款。
这样的事情:
|----------|----------|
| INV_Name | Tax_List |
|----------|----------|
| Popcorn | 9,10 |
| Movie | | <- No tax for this item
|----------|----------|
我的最后一次尝试就是这个:
SELECT
*,
IFNULL(GROUP_CONCAT(STX_Id), "") as `Tax_List`
FROM ___Inventory
LEFT JOIN ___ApplicableTaxesInventory ati
ON ati.ATX_INVId = i.INV_Id
LEFT JOIN ___SalesTaxes st
ON st.STX_Id = ati.ATX_STXId
我不知道为什么它不起作用。
如果需要,请参阅此SQLFiddle以帮助您:
http://sqlfiddle.com/#!9/a4539
感谢。
答案 0 :(得分:0)
试试这个。使用子查询
select INV_Name,
(select group_concat(STX_Amount) from ___ApplicableTaxesInventory b inner join
___SalesTaxes c on b.ATX_STXId=c.STX_Id where a.INV_Id=b.ATX_INVId
) as taxlist from ___Inventory a
答案 1 :(得分:0)
select i.INV_Id,GROUP_CONCAT(ATX_STXId) 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 i.INV_Id
或
select i.INV_Id,GROUP_CONCAT(ATX_STXId) from
Inventory i
left join ApplicableTaxesInventory ati ON ati.ATX_INVId = i.INV_Id
group by i.INV_Id
不需要离开加入SalesTaxes
表。
结果:
|----------|----------|
| INV_Name | Tax_List |
|----------|----------|
| Popcorn | 9,8 |
| Movie | |
|----------|----------|
Popcorn Tax_List不是9,10
这是表格创建和数据。
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `ApplicableTaxesInventory`
-- ----------------------------
DROP TABLE IF EXISTS `ApplicableTaxesInventory`;
CREATE TABLE `ApplicableTaxesInventory` (
`ATX_INVId` int(11) DEFAULT NULL,
`ATX_STXId` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `ApplicableTaxesInventory`
-- ----------------------------
BEGIN;
INSERT INTO `ApplicableTaxesInventory` VALUES ('34', '8'), ('34', '9');
COMMIT;
-- ----------------------------
-- Table structure for `Inventory`
-- ----------------------------
DROP TABLE IF EXISTS `Inventory`;
CREATE TABLE `Inventory` (
`INV_Id` int(11) NOT NULL,
`INV_Name` varchar(32) DEFAULT NULL,
PRIMARY KEY (`INV_Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `Inventory`
-- ----------------------------
BEGIN;
INSERT INTO `Inventory` VALUES ('34', 'Popcorn'), ('35', 'Movie');
COMMIT;
-- ----------------------------
-- Table structure for `SalesTaxes`
-- ----------------------------
DROP TABLE IF EXISTS `SalesTaxes`;
CREATE TABLE `SalesTaxes` (
`STX_Id` int(11) NOT NULL,
`STX_TaxeName` varchar(32) DEFAULT NULL,
`STX_Amount` double(10,4) DEFAULT NULL,
PRIMARY KEY (`STX_Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `SalesTaxes`
-- ----------------------------
BEGIN;
INSERT INTO `SalesTaxes` VALUES ('8', 'Tax_1', '5.0000'), ('9', 'Tax_2', '5.0000');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;