View Full Version : 'Linking' two mysql tables

11-14-2006, 11:48 AM
This is one of those questions that involves PHP and MySQL so I wasn't sure where to put it. Anyway....What I want to do if possible is effectively 'link' two bits of data from two separate tables together.

So...my two tables are as follows:

groups table:
ID | Groupname
1 | Admin
2 | Editors
3 | Users

user table:
ID | Group
1 | 1
2 | 3
3 | 2

So, I want to display a user's information from the user table and then match the group number up with the ID in the groups table and then display the groupname.

e.g User ID 1 is in group 1, therefore his output would be something like:

user id = 1
group = Admin

Hope its clear what I want to do ;)


11-14-2006, 12:08 PM
i gues what u need is a sql statement that goes something like this

select gt.groupname from user_table ut, group_table gt where ut.group=gt.id

by the way, u may av to rename the table field with the name 'group' to something like 'grp' coz it may cause u a lot of headaches later - 'group' is a mysql reserve word.

11-14-2006, 06:18 PM
Hi Alex

for future reference, ask your database questions in the mysql forum. if you are having trouble getting the results of your database query to appear correctly using PHP (or whatever) then post the question in that forum.