skip to Main Content

I’m trying to get data from "users" table into ‘user_id’ and ‘profile_fullname’ rows from "profiles" table.

For example if a row has an id of 1 in the "users" table, then the ‘user_id’ should also be 1.

Same with the ‘profile_fullname’.

Here’s the code:

CREATE TABLE users (
    user_fullname VARCHAR(100) NOT NULL,
    user_email VARCHAR(60) NOT NULL,
    user_password VARCHAR(60) NOT NULL, 
    id SERIAL PRIMARY KEY
);

CREATE TABLE profiles (
    user_id INTEGER REFERENCES users(id),
    profile_fullname VARCHAR(60),
    profile_headline TEXT,
    profile_professional_headline TEXT,
    profile_website_link VARCHAR(200),
    profile_twitter_link VARCHAR(200),
    profile_facebook_link VARCHAR(200),
    profile_linkedin_link VARCHAR(200),
    profile_youtube_link VARCHAR(200),
    id SERIAL PRIMARY KEY
);

I’ve tried researching the problem, but still seems to be unclear for me how to do it.

So how can i solve it?

2

Answers


  1.  select u.id,prof.profile_fullname
     from users as u
     join profiles as prof on u.id=prof.user_id
     where u.id=1 
    

    you need this one?

    Login or Signup to reply.
  2. I assume your profiles table is empty and you are wanting to populate it with rows from the users table:

    INSERT INTO profiles 
        SELECT id as user_id , user_fullname  as profile_fullname
        FROM users 
    

    https://dba.stackexchange.com/questions/2973/how-to-insert-values-into-a-table-from-a-select-query-in-postgresql

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search