question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Diferent results on SELECT query

See original GitHub issue

I have a table “users” whit this rows:

user_id user_name
1 Jhon
2 Paul

And other table “tests” with this rows:

test_id created_by_user_id modified_by_user_id
1 1 2

at psql shell, I send the following query:

SELECT test_id, created.user_name, modified.user_name FROM tests LEFT JOIN users as created ON tests.created_by_user_id = created.user_id LEFT JOIN users as modified ON tests.modified_by_user_id = modified.user_id

as expected, I have thr following result:

test_id created modified
1 Jhon Paul

but using pg-node whit the same query as:

const result = await client.query('SELECT test_id, created.user_name, modified.user_name FROM tests LEFT JOIN users as created ON tests.created_by_user_id = created.user_id LEFT JOIN users as modified ON tests.modified_by_user_id = modified.user_id')

in result.rows I have this:

test_id created modified
1 Jhon Jhon

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
joelmukuthucommented, Sep 14, 2019

Hi, that’s happening because you haven’t used aliases in your return fields. This is what the query returns (both in psql and when it’s run by node-pg):

test_id  | user_name | user_name 
---------+-----------+-----------
       1 | Jhon      | Paul

When node-pg parses that row, it finds two columns both named user_name, and because those get assigned to an object, the second one replaces the first one. So what you get in result.rows is actually:

 [ { test_id: 1, user_name: 'Paul' } ]

You could either:

  1. add aliases to the return fields in your query (created.user_name AS created_by, modified.user_name AS modified_by):

const result = await client.query({ 
  text: 'SELECT test_id, created.user_name AS created_by, modified.user_name AS modified_by FROM tests LEFT JOIN users as created ON tests.created_by_user_id = created.user_id LEFT JOIN users as modified ON tests.modified_by_user_id = modified.user_id'
});

// result.rows:
// [ { test_id: 1, created_by: 'Jhon', modified_by: 'Paul' } ]
  1. or configure the query to return arrays instead of objects (rowMode: 'array'):
const result = await client.query({ 
  text: 'SELECT test_id, created.user_name, modified.user_name FROM tests LEFT JOIN users as created ON tests.created_by_user_id = created.user_id LEFT JOIN users as modified ON tests.modified_by_user_id = modified.user_id',
  rowMode: 'array'
});

// result.rows:
// [ [ 1, 'Jhon', 'Paul' ] ]
0reactions
joelmukuthucommented, Sep 15, 2019

Great to hear, you’re welcome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Same query producing different results - sql - Stack Overflow
I see only one query. I would point out that underlying data can change, and so the same query can generate different results....
Read more >
How to handle an equivalent query producing different results ...
If you have an “equivalent query” producing different results, then it is not an equivalent query. Ideally, you would break the query down...
Read more >
About the same SQL Server query get different result in two PCs
Hello guys , there is a problem when I run my SQL query. When I run the follow query in my PC ,....
Read more >
Same query, same data, different results between S... - 89917
I am running the following query on the same data (same tables, and the same number of records on those - 89917.
Read more >
Why am I getting different results from a query vs. a view
I need to create two views (because MySQL won't let me have a subquery in a view). My first query sets up the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found