i want to join two table in mysql node js.example POST and Users table.i want to get all post that belong to one user.please help me thanks
CodePudding user response:
SELECT * FROM POST INNER JOIN Users ON POST.user = Users.user;
this above code will combine the 2 tables which has the same user in both tables
CodePudding user response:
If I understand your problem, you want to join two table where a value correspond. The solution for that would be to use a JOIN clause.
Let's make an example,
Here's my table1:
| id | firstname |
|---|---|
| 1 | Alex |
| 2 | John |
Here's my table2:
| id | lastname |
|---|---|
| 1 | Brown |
| 2 | Davis |
So here what I want is to get the full name of a user by their id, in this case I would do:
SELECT table1.firstname, table2.lastname FROM table1 INNER JOIN table2 ON table1.id = table2.id
SELECT the values I want but I add the table name infront of the value
FROM the first table I want to join INNNER JOIN the second table I want to join
ON the table1.value that correspond to table2.value
And this would be my result:
| id | firstname | lastname |
|---|---|---|
| 1 | Alex | Brown |
| 2 | John | Davis |
I hope that you understand what todo, it's the first time I help someone here and I'm not really sure if I did good but I think it will still help you.
If you want more information about JOIN clause: Source: w3schools
