user2307958
user2307958

Reputation: 351

Get value from another table

I have 2 tables like this:

Table1 doc_id (1) doc_folder (1010) doc_title (invoice 2020)

Table2 file_id (1) file_doc_id (1) file_name (invoice.pdf)

Now, i perform a query to get all titles:

    SELECT * FROM Table1
    WHERE doc_folder='1010'

I'm like to get a file name from Table2 and have result like this:

FOLDER | TITLE     | FILE NAME 
1010     invoice     invoice.pdf

How to get filename from Table2

TKS ALL

Upvotes: 0

Views: 44

Answers (3)

maciej-ka
maciej-ka

Reputation: 575

SELECT * FROM Table1 
JOIN Table2 ON file_doc_id = doc_id
WHERE doc_folder='1010'

In this case table names don't have to be prefixed, because all columns have different names. However, it is considered good practice to add them, which would look this way:

SELECT * FROM Table1 t1
JOIN Table2 t2 ON t2.file_doc_id = t1.doc_id
WHERE t1.doc_folder='1010'

Upvotes: 0

Onur Gazioğlu
Onur Gazioğlu

Reputation: 511

SELECT Table1.*, Table2.file_id, Table2.file_name
FROM            Table1 INNER JOIN
                     Table2 ON Table1.doc_id = Table2.file_doc_id
WHERE        (Table1.doc_folder = '1010')

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

select table1.*, table2.file_name from table1
inner join table2 on table2.file_doc_id = table1.doc_id
where table1.doc_folder='1010'

You can use table. doc_folder, table.doc_title instead of table1.* if you do not need to fetch all columns from table1

Upvotes: 1

Related Questions