You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've seen this feature in PHP drivers (PDO fetch_style parameter PDO::FETCH_NUM) where one could refer to the columns in the result set by their index position within the "fields" array. I was not able to find this in node-mysql driver. The use-case for it is when someone runs a join on 2 tables, and both table contain the same field name, the current row object only contains one value because field names collide. I know that one may use aliasing to distinguish the 2, but that can be problematic/cumbersome particularly when faced with pragmatically generated queries.
Referring to columns by first analyzing the fields array is an elegant solution for this. Currently when one runs a join with select *, the key count in each row is different that the count on fields array.
conn.query('select * from A join B...',function(err,result,fields){if(Object.keys(result[0]).length!=fields.length){console.log('Field name collision occured');}});
This creates a problem as the fields array becomes "misaligned" with the result row, and therefore makes any references between the field details and actual values difficult.
I would suggest something along the lines of an options object passed optionally after values:
conn.query('select * from A join B ON A.id=B.A_id WHERE A.id=?',[1],{useArrayRows: true},function(err,result,fields){varcolumnIndex=0;console.log('The value of '+fields[columnIndex].orgTable+'.'+fields[columnIndex].name+' is 'result[0][columnIndex]);});
Thanks
The text was updated successfully, but these errors were encountered:
Hi,
I've seen this feature in PHP drivers (PDO fetch_style parameter PDO::FETCH_NUM) where one could refer to the columns in the result set by their index position within the "fields" array. I was not able to find this in node-mysql driver. The use-case for it is when someone runs a join on 2 tables, and both table contain the same field name, the current row object only contains one value because field names collide. I know that one may use aliasing to distinguish the 2, but that can be problematic/cumbersome particularly when faced with pragmatically generated queries.
Referring to columns by first analyzing the fields array is an elegant solution for this. Currently when one runs a join with select *, the key count in each row is different that the count on fields array.
This creates a problem as the fields array becomes "misaligned" with the result row, and therefore makes any references between the field details and actual values difficult.
I would suggest something along the lines of an options object passed optionally after values:
Thanks
The text was updated successfully, but these errors were encountered: