mysql2 icon indicating copy to clipboard operation
mysql2 copied to clipboard

obtaining table and column name in results.fields

Open thvv opened this issue 2 years ago • 2 comments

A JOIN query can return values from columns from more than one table. It can happen that two different tables have columns of the same name, and sometimes I want to know which table a value came from.

One way to do this would be an option that put the table name and column in the results.fields arrays. Alternatively mysql2 could support a new array results.fieldsx that returned table1.field1 and so on

I can do this in Perl's DBD::mysql using @tables = @{$sth->{'mysql_table'}} I can do this in Golang by setting ColumnsWithAlias: true when I open github.com/go-sql-driver/mysql

thvv avatar Aug 26 '22 17:08 thvv

I have submitted a patch to do this, as the raw data is actually there. It hasn't gained traction though. A horrible, but simple solution is to do something like this

  SELECT c.site_name as 'c.site_name',
               d.site_name as 'd.site_name'
  FROM customer as c, distribution as d
  

Gets worse quickly, if there are lots of fields, but it does work

rbur004 avatar May 28 '23 04:05 rbur004

thanks Rob.

On May 28, 2023, at 6:29 AM, Rob Burrowes @.***> wrote:

I have submitted a patch to do this, as the raw data is actually there. It hasn't gained traction though. A horrible, but simple solution is to do something like this SELECT c.site_name as 'c.site_name', d.site_name as 'd.site_name' FROM customer as c, distribution as d

Gets worse quickly, if there are lots fields, but it does work — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

thvv avatar May 28 '23 13:05 thvv