Mathias Fyrst Jakobsen
Mathias Fyrst Jakobsen

Reputation: 139

Joining table ids

Hi there

I got 3 tables

cc_host_uploads contains
upload_id       file_id

cc_host_files contains
file_id         user_id

cc_host_users contains
user_id

as of right now there are a link (id's) between the 3 tables, what i want is a link between the two.

I want the cc_host_uploads table to have the user_id insted of the file_id... so i need to join them somehow.

Can anybody enlighten me? Thanks!

Upvotes: 0

Views: 29

Answers (2)

Martin Lyne
Martin Lyne

Reputation: 3065

I think this is what you mean? Getting the user of the upload via the files?

SELECT upload.*, user.id from cc_hosts_uploads upload
INNER JOIN cc_hosts_files files on upload.id = files.upload_id
INNER JOIN cc_host_users user on user.id = files.user_id

Upvotes: 1

John Woo
John Woo

Reputation: 263723

Use INNER JOIN for this. Assuming all cc_host_uploads belongs to a certain user user_id.

SELECT  a.*, c.*
FROM    cc_host_users a
        INNER JOIN cc_host_files b
            ON a.user_id = b.user_id
        INNER JOIN cc_host_uploads c
            ON b.file_id = c.file_id

Upvotes: 1

Related Questions