How to create a column in a table from a Select

Asked

Viewed 1,146 times

0

I have the table users and the table posts, need to copy the same information from a column of table users to a column of table posts, for example I have this code:

SELECT posts.*, users.username
FROM posts
  inner join users on (posts.user_id = users.id )

which brings me a query of exactly what I need, but in my php project I need this column to be created, to be able to visualize it by my html.

One more example to help in understanding my question, if I run the code:

ALTER TABLE articles ADD COLUMN user_id INT(11);

he can create exactly what I need, but with the field "id" which is in the users table, but only with the "id", if I make the same code with the field I need, which is the username: "user_username", returns me null.

  • 1

    I’m not sure I understand, but your case seems to be solved by creating a View

  • It was not clear the question, what is your question exactly ? Popular the table with the code information of the user who wrote the article ? What kind of relationship the Articles table has with that of posts and users?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.