saihenjin
saihenjin

Reputation: 27

Cross referencing tables in a mySQL query?

I have two tables. One is a table of users with a unique id field, and the other is a table of data, with a column that holds the user id of whoever generated that piece of data.

I want to do something like SELECT data,genned_by FROM datatable; but I want to replace the results for genned_by with SELECT username FROM users WHERE id = genned_by

So that the results from the query changes the userid into a username that corresponds with the other table.

I did some research and figured INNER JOIN might be what I'm looking for, but I'm left very unsure of how to use it after reading it. Help?

Upvotes: 1

Views: 3421

Answers (2)

Nir Alfasi
Nir Alfasi

Reputation: 53535

SELECT datatable.data,users.username 
FROM datatable, users
WHERE users.id = datatable.genned_by

Upvotes: 2

nymph
nymph

Reputation: 66

Try to use

SELECT d.data, u.username FROM database d INNER JOIN user u ON u.id=d.genned_by

Hope it helps you

Upvotes: 4

Related Questions