0

我需要将以下查询输出为 csv。

我可以轻松编写 php 逻辑将行从我的 group_concat 列转置为列

但是,我热衷于将尽可能多的数据部分保留在数据库中,并尽量减少 php 端的操作。

我正在尝试查询中 group_concat 下面的两列。

问题是 life_stage 列的丰度值也返回。如果除了操作 group_concat 键值之外没有其他办法,那很好,我只是想仔细检查一下。提前致谢

SELECT 
`tr`.`tr_id_pk` as 'RecordKey',
`t`.`tax_name` as `TaxonName`,
`tr`.`tr_date` as 'Date',
`s`.`si_name` as 'SiteName',
`tr`.`tr_grid_reference` as 'GridReference',
`tr`.`tr_is_site_grid` as 'IsSiteGrid',
`r`.`rec_name` as 'Recorder',
`r`.`rec_email` as 'RecorderEmail',
`tr`.`tr_comment` as 'RecordComment',
`tr`.`tr_last_update` as 'LastUpdated',
`tr`.`tr_form_key` as 'FormKey',
`c`.`co_name` as 'County',
`vc`.`vc_name` as 'ViceCounty',
`h`.`hab_name` as 'Habitat',
GROUP_CONCAT(DISTINCT CONCAT_WS('=', `ra`.`ra_name`, `rad`.`rad_value`)) as 'RecordAttributeKeyValuePairs',
`rad`.`rad_value` AS `abundance`,
`rad`.`rad_value` AS `life_stage`
FROM
`taxon_record`as `tr`
    INNER JOIN
`taxon`as `t` ON `tr`.`tax_id_fk` = `t`.`tax_id_pk`
    INNER JOIN
`recorder`as `r` ON `tr`.`rec_id_fk` = `r`.`rec_id_pk`
    INNER JOIN
`site`as `s` ON `tr`.`si_id_fk` = `s`.`si_id_pk`
    LEFT JOIN
`county`as `c` ON `tr`.`co_id_fk` = `c`.`co_id_pk`
    LEFT JOIN
`vice_county`as `vc` ON `tr`.`vc_id_fk` = `vc`.`vc_id_pk`
    LEFT JOIN
`habitat`as `h` ON `tr`.`hab_id_fk` = `h`.`hab_id_pk`
    LEFT JOIN
(`record_attribute_data`as `rad`
INNER JOIN `record_attribute`as `ra` ON (`rad`.`ra_id_fk` = `ra`.`ra_id_pk`)) ON (`tr`.`tr_id_pk` = `rad`.`tr_id_fk`)
WHERE
`r`.`rec_email` = 'some_email@somewhere.com'
GROUP BY `tr`.`tr_id_pk`; 
4

2 回答 2

0

问题是您以不同的名称两次获取同一列吗?:

`rad`.`rad_value` AS `abundance`,
`rad`.`rad_value` AS `life_stage`

看起来你总是会以这种方式获得丰富和 life_stage 相同的东西。

于 2012-05-02T10:17:15.520 回答
0

您希望执行的操作称为“透视”您的数据,并且某些其他 RDBMS 对此具有本机支持,但 MySQL 不支持(设计上,开发人员认为此类操作属于表示层)。

但是,正如您所暗示的,您可以构建一个相当糟糕的 MySQL 查询来手动执行旋转操作(需要为每个输出列将属性表连接到查询一次):

SELECT tr.tr_id_pk, abundance, life_stage -- etc.
  FROM taxon_record AS tr
  LEFT JOIN (
    SELECT rad.tr_id_fk, rad.rad_value AS abundance
    FROM
      record_attribute_data ra JOIN record_attribute rad
        ON rad.ra_id_fk = ra.ra_id_pk
    WHERE ra.ra_name = 'abundance'
  ) AS tAbundance ON tAbundance.tr_id_fk = tr.tr_id_pk
  LEFT JOIN (
    SELECT rad.tr_id_fk, rad.rad_value AS life_stage
    FROM
      record_attribute_data ra JOIN record_attribute rad
        ON rad.ra_id_fk = ra.ra_id_pk
    WHERE ra.ra_name = 'life_stage'
  ) AS tLife_Stage ON tLife_Stage.tr_id_fk = tr.tr_id_pk
  -- etc.

如果您选择走这条路,您可以通过使用 PHP 中的循环构造或 MySQL 中的准备好的语句生成此查询,从而使您的生活稍微轻松一些。

于 2012-05-02T10:17:24.567 回答