如何更改此查询返回多个记录以返回包含原始记录串联的单个记录?

时间:2017-10-23 10:06:52

标签: mysql sql database rdbms

我不是那么进入数据库而且我遇到了以下问题。我正在使用 MySql

我有这个问题:

SELECT 
        LSSN.livestock_species_name AS species_name,
        LSFT.feed_type_name         AS feed_type_name,
        LSFT.description            AS feed_description
FROM LivestockDetails AS LSD
INNER JOIN LivestockSpecies AS LSS
      ON LSD.live_stock_species_id = LSS.id
INNER JOIN LivestockSpeciesName AS LSSN
      ON LSS.id = LSSN.livestock_species_id 
INNER JOIN Languages AS LNG
      ON LSSN.language_id = LNG.id   
INNER JOIN LsFeedDetails AS LSFD
      ON LSD.ls_feed_details_id = LSFD.id
INNER JOIN LsFeedTypes AS LSFT
      ON LSFD.ls_feed_types_id = LSFT.id
WHERE LSD.live_stock_species_id = 3
AND LSD.ls_area_id = 2
AND LSD.ls_action_type_id = 1
AND LSD.ls_message_id is null
AND LNG.id = 1

返回这些记录:

species_name            feed_type_name                         feed_description
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Local Cows              Rangeland                              Grasses         
Local Cows              Crop residues                          Maize and sorghum Stover, rice, wheat, and sugar bean straw, parts of banana plants, wheat straw
Local Cows              Improved grasses                       e.g. Napier grass
Local Cows              Browsing and herbaceous legumes        Browsing (largely during dry season) and herbaceous legumes (year round)
Local Cows              Planted pastures                       Napier, etc...  
Local Cows              Assorted weeds                         Assorted weeds  

好的,这些检索到的数据很好,但我必须以下列方式修改此查询:

1)它返回一条记录,其中包含上一个查询检索到的记录中包含的所有信息的串联。

2)连接必须是这样的:

feed_type_name RECORD-1 + (feed_description RECORD-1) + ";" +feed_type_name RECORD-2 + (feed_description RECORD-2) + ";" + feed_type_name RECORD-3 + ";" + (feed_description RECORD-3) + ..............................

因此,使用以前检索到的数据作为示例:

Rangeland (Grasses) ; Crop residues (Maize and sorghum Stover, rice, wheat, and sugar bean straw, parts of banana plants, wheat straw) ; Improved grasses  (e.g. Napier grass) ; ............................................

我真的不知道如何修改我的原始查询来创建一个类似这样的查询。你能帮帮我吗?

3 个答案:

答案 0 :(得分:1)

GROUP BY" species_name"并使用GROUP_CONCAT(feed_type_name SEPARATOR',')功能。

https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat

答案 1 :(得分:1)

使用 GROUP_CONCAT 并使用 GROUP BY 按种类聚合:




  SELECT
 LSSN.livestock_species_name AS species_name,
 GROUP_CONCAT(CONCAT(LSFT.feed_type_name,'(',LSFT.description,')')SEPARATOR';')AS text
 FROM LivestockDetails AS LSD
 INNER JOIN LivestockSpecies AS LSS
 ON LSD.live_stock_species_id = LSS.id
 INNER JOIN LivestockSpeciesName AS LSSN
 ON LSS.id = LSSN.livestock_species_id
 INNER JOIN语言作为LNG
 ON LSSN.language_id = LNG.id
 INNER JOIN LsFeedDetails AS LSFD
 ON LSD.ls_feed_details_id = LSFD.id
 INNER JOIN LsFeedTypes AS LSFT
 ON LSFD.ls_feed_types_id = LSFT.id
 WHERE
 LSD.live_stock_species_id = 3
 AND LSD.ls_area_id = 2
 AND LSD.ls_action_type_id = 1
 AND LSD.ls_message_id为null
 AND LNG.id = 1
 GROUP BY
 LSSN.livestock_species_name;
  



答案 2 :(得分:0)

可以按照以下方式完成

SELECT  feed_type_name +'('+feed_description +') ; ' as [text()] from 
.....
.....
.....

FOR XML PATH('')