我有特定日期的可用组件数量和库存历史成本的显示数据。它使用以下查询工作正常。
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
答案 0 :(得分:9)
请看以下示例:
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&GT;选择国家,国家组缩写从缩写;
+--------------------------+--------------+
| 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&GT;选择GROUP_CONCAT(国家),缩写来自国家组的缩写;
+--------------------------+--------------+
| 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()函数click here
的更多信息答案 1 :(得分:2)
查询下方显示重复数据:
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