5

I have two tables in Postgres database. In each table there is a column which represent same number. I have tried few join queries to join both tables with similar column numbers but none of them are giving me the expected output.

user_id column from Table 1 is equal to Id column in Table 2

How can join these two tables?

I have tried below and some other queries as well but it didn't get what I wanted

SELECT members.access_level, members.user_id FROM members INNER JOIN users ON members.user_id = users.id;

Tables columns looks like below,

Members table

id |access_level |source_id |user_id |type

Users Table

id |email |name |username 

Query output should look as below:

username |name |email |access_level
1
  • The query you described seems like it should work. When you say you're not getting the expected output, do you mean that data from the two tables does not appear to match or that you don't see the columns you want? Commented Feb 7, 2019 at 19:31

2 Answers 2

6
SELECT  u.username
        , u.name
        , u.email
        , m.access_level
FROM users u
    JOIN members m ON (u.id = m.user_id)
;

If you want users that are not included in the members table you can join with a LEFT JOIN

To address your question asked in the comments I believe you'd be looking for something like the following:

UPDATE members SET access_level = 'dev' WHERE access_level = '30';

This is assuming that the column is already of type text. Otherwise, you'll need to change the data type first using the following:

ALTER TABLE members
    ALTER access_level SET DATA TYPE text;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you...In the access_level column there are predefined numbers for entire column like 30, 40, 50 ...I want replace these numbers with string such as Dev, Main, Owner..Is this something possible with query?
Do you only want to update the values for members that are also users or all members? Also, what is the data type of the access_level column currently? It's likely you'll want an UPDATE query.
access_level column is in members table and its numeric data type..it has 30,40 and 50 values ..I want them to replace with Dev, Main and Owner
1
 SELECT users.*, members.user_id, members.acces_level 
 FROM   members 
 LEFT JOIN users 
 WHERE users.id = members.user_id

1 Comment

Thank you, the [table].* syntax was just what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.