4

我已经显示了截至特定日期的可用组件数量和库存历史成本的数据。使用以下查询可以正常工作。

SELECT s.part_id,
       sum(s.updated_quantity),
       p.item_code,
       sum(
             (SELECT (s.updated_quantity * cost)
              FROM inventory
              WHERE inventory.id=s.inv_id)) AS tcost
FROM status_history AS s,
     inventory AS i,
     part_master AS p
WHERE s.action='add'
  AND DATE(s.date_created)<='2013-04-09'
  AND i.currency_id=1
  AND s.inv_id=i.id
  AND s.part_id=p.id
GROUP BY s.part_id

我还想在单个字段中显示用逗号分隔的组件的位置名称。为了得到我想要的结果,我尝试了下面的查询,但它只返回一个位置名称,而不是多个位置名称的逗号分隔字符串。

SELECT s.part_id,
       sum(s.updated_quantity),
       p.item_code,
       sum(
             (SELECT (s.updated_quantity * cost)
              FROM inventory
              WHERE inventory.id=s.inv_id)) AS tcost,
       CONCAT_WS(',',
                   (SELECT name
                    FROM location_master
                    WHERE id=i.location_id)) AS LOCATION
FROM status_history AS s,
     inventory AS i,
     part_master AS p
WHERE s.action='add'
  AND DATE(s.date_created)<='2013-04-09'
  AND i.currency_id=1
  AND s.inv_id=i.id
  AND s.part_id=p.id
GROUP BY s.part_id
4

2 回答 2

12

看下面的例子:

mysql> select Country,Abbreviation from Country;
+--------------------------+--------------+
| Country                  | Abbreviation |
+--------------------------+--------------+
| INDIA                    | ID           |
| Canada                   | CAN          |
| Australiya               | AUS          |
| United Kingdom           | UK           |
| United States Of America | USA          |
| PAKISTAN                 | PAK          |
| MILAN                    | Panchal      |
| MILAN                    | Panchal      |
| JAPAN                    | JP           |
+--------------------------+--------------+
9 rows in set (0.00 sec)

Panchal 有 2 条记录

按缩写分组将仅显示 1 条记录。

mysql> select Country,Abbreviation from Country group by Abbreviation;

+--------------------------+--------------+
| Country                  | Abbreviation |
+--------------------------+--------------+
| Australiya               | AUS          |
| Canada                   | CAN          |
| INDIA                    | ID           |
| JAPAN                    | JP           |
| PAKISTAN                 | PAK          |
| MILAN                    | Panchal      |
| United Kingdom           | UK           |
| United States Of America | USA          |
+--------------------------+--------------+

Country 上的 GROUP_CONCAT 将显示逗号分隔值。

mysql> select GROUP_CONCAT(Country),Abbreviation from Country group by Abbreviation;

+--------------------------+--------------+
| GROUP_CONCAT(Country)    | Abbreviation |
+--------------------------+--------------+
| Australiya               | AUS          |
| Canada                   | CAN          |
| INDIA                    | ID           |
| JAPAN                    | JP           |
| PAKISTAN                 | PAK          |
| MILAN,MILAN              | Panchal      |
| United Kingdom           | UK           |
| United States Of America | USA          |
+--------------------------+--------------+

从顶部看第 6 条记录。因为它包含逗号分隔的值。

要了解有关 GROUP_CONCAT() 函数的更多信息,请单击此处

于 2013-04-12T10:58:39.187 回答
3

下面的查询显示重复数据:

select 
s.part_id,
sum(s.updated_quantity),
p.item_code,
sum(
     (select (s.updated_quantity * cost) from inventory where inventory.id=s.inv_id)) 
      as tcost,
GROUP_CONCAT(name) as location 
from status_history as s,
inventory as i,
part_master as p,
location_master as l 
where s.action='add' and 
DATE(s.date_created)<='2013-04-09' and 
i.currency_id=1 and 
s.inv_id=i.id and 
s.part_id=p.id and 
l.id=i.location_id 
group by s.part_id

下面的查询显示不同的数据:

select 
s.part_id, 
sum(s.updated_quantity),
p.item_code,
sum(
     (select(s.updated_quantity * cost) from inventory where inventory.id=s.inv_id))
       as tcost,
GROUP_CONCAT(distinct(name)) as location 
from status_history as s,
inventory as i,
part_master as p,
location_master as l 
where s.action='add' and 
DATE(s.date_created)<='2013-04-09' and 
i.currency_id=1 and 
s.inv_id=i.id and 
s.part_id=p.id and 
l.id=i.location_id 
group by s.part_id
于 2013-04-12T11:28:17.913 回答