Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:30:14+00:00 2026-06-03T03:30:14+00:00

I need to output the following query as csv. I can easily write php

  • 0

I need to output the following query as csv.

I can easily write php logic to transpose the rows to columns from my group_concat column

However I am keen to keep as much of the data part in the database and minimize the manipulations on the php side.

I am experimenting with the two columns below the group_concat in the query.

The problem is the abundance value also returns for life_stage column. If there is no way around this other than manipulating the group_concat key values then that’s fine, I just wanted to double check. Thanks in advance

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`; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:30:16+00:00Added an answer on June 3, 2026 at 3:30 am

    What you wish to do is known as “pivoting” your data and is something for which some other RDBMS have native support, but MySQL does not (by design, as the developers feel that such manipulations belong in the presentation layer).

    However, as you’ve hinted at, you can construct a rather horrible MySQL query to perform the pivoting operation manually (one needs to join the attributes tables to the query once for each output column):

    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.
    

    If you choose to go down this path, you can make your life slightly easier by generating this query using either a looping construct in PHP or a prepared statement in MySQL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code I need to print output from the variables $stout ,
I need help doing the following: a preprocessor macro label(x) shall output #x, e.g.,
I was following this tutorial . I need to use a php file's ouput
I need to output a file in the format of a Word document from
I am using the following code for display questions and answers from admin. <?php
I have the following query in mysql 5.1.41: select distinct table_schema from information_schema.tables where
I currently have the following query: SELECT group_concat(DISTINCT usrFirst, usrLast) as receiver_name //etc When
I have a query that produces the following output in a nested set structure:
I have the following query running against a mysql database: select value from fact_data
I would like to output the following table from my database DateTime | Measure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.