0

I'am stuck in an sql query and I didn't know how to do it, well, first, I Have two tables

Comments :
id INT A.I
date_created  INT
update_id INT
comment TEXT

and Another Table

users
id
username
firstname
lastname

I Managed to display the comment and the date because it's in the same table, but I wanna join the username with the comment, and I tried to do the query with Navicat but I did not worked, Please if anyone have an idea, just comment it , and thank you

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 1
    your table miss a foreigner key : you need the id of the user in the comment table – Blag Apr 07 '18 at 10:09
  • 1
    You need to know how those tables are related, the *Foreign Key* definitions? When the `update_id` contains user ids it's a `from comments as c join users as u on c.update_id = u.id` – dnoeth Apr 07 '18 at 10:10

2 Answers2

1

Let suppose you have two tables like :

Users

UserID | INT
Name   | VARCHAR(250)
EMail  | VARCHAR(250)

Comments

CommentsID | INT 
UserID     | INT
Comments   | VARCHAR(MAX)

The join will be like the following

SELECT * FROM Comments c INNER JOIN users u ON c.UserID=u.UserID

In Your case query will be like:

SELECT * FROM Comments c INNER JOIN users u ON c.update_id =u.id
Faraz Babakhel
  • 654
  • 5
  • 14
0

Query For you

SELECT * 
FROM Comments T1 
INNER JOIN users T2 
ON T1.update_id = T2.id

SQL INNER JOIN

The INNER JOIN keyword selects records that have matching values in both tables.

INNER JOIN Syntax

SELECT column_name(s)
FROM table1
INNER JOIN table2 
ON table1.column_name = table2.column_name;

To Learn more about join Link

Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27