views:

28

answers:

1

If i have two tables like

user table-"u"

userid | name
 1     |  lenova
 2     |  acer
 3     |  hp

pass table-"p"

userid | password
 1     | len123
 2     | acer123
 3     | hp123

as for as i learnt from tutorials I can join these 2 tables using many joins available in mysql as said here

If i have a table like

role table-"r"

roleid | rname
 1     | admin
 2     | user
 3     | dataanalyst

token table-"t"

tokenid| tname
 1     | xxxx
 2     | yyyy
 3     | zzzz

tole_token_association table-"a"

roleid | tokenid
 1     |  1
 1     |  2
 3     |  1
 3     |  3
 3     |  1

I have to make a join such that I have to display a table which corresponds like this "rolename" has all these tokens.How to make this? I am confused. Is it possible to make a join? I am liking mysql a lot. I wish to play with queries such that not playing. I want to get well versed. Any Suggestions Please?

+1  A: 

It's easiest to see when the column names that need to be joined are named identically:

SELECT r.rname,
       t.tname
  FROM ROLE r
  JOIN ROLE_TOKEN_ASSOCIATION rta ON rta.roleid = r.roleid
  JOIN TOKEN t ON t.tokenid = rta.tokenid

This will return only the roles with tokens associated. If you have a role that doesn't have a token associated, you need to use an OUTER join, like this:

   SELECT r.rname,
          t.tname
     FROM ROLE r
LEFT JOIN ROLE_TOKEN_ASSOCIATION rta ON rta.roleid = r.roleid
     JOIN TOKEN t ON t.tokenid = rta.tokenid

This link might help -- it's a visual representation of JOINs.

OMG Ponies
If names are identical you can use NATURAL JOIN without the ON clause
MatTheCat
@MatTheCat: You can, but not all databases support the NATURAL JOIN syntax, and I prefer to be explicit about my join criteria. Partly because that's what the OP is asking about--how/what columns to JOIN on. Doesn't help if you hide that information from someone...
OMG Ponies
Ok I just noticed this because it was MySQL. I like save many caracters when my queries become longer ^^
MatTheCat
@OMG thank u very much i tried this its working great. suppose if i dont have a table rta and i have the tables r and t.is it possible that i can create a table and implement the query saying to Db that tokenid and roleid should be the 2 columns and join shoild be like this. ie cretaing a table and implmenting a join using a query?
Code 'N' Weed
@Code 'N' Weed: If I understand correctly, yes--you can create a derived table (AKA inline view) that represents what `rta` does to link the two tables together. The fundamental issue is identical column values (including data type) in the tables you want to relate--if they aren't immediately available (like in your example), then you have to link/join the tables in the order of the column associations necessary to get the data you want.
OMG Ponies
@OMG thsnks. if i create such a derived table will it erase the values each time when the query runs? so which is the better idea following the above method or making derived table. it seems to be a virtual table right. when going to complex tables with more columns and no relation i am afraid how to make joins.
Code 'N' Weed
@Code 'N' Weed: Yes, the derived table will be re-created for every query that runs using it, so it is best to store the data in a real table if possible.
OMG Ponies
@OMG if i want to join 2 tables r and t and i don want to have rta table. using r and t i want to join both and just i want to display the values. simply in a dialog box which i specify in my JS. its simply taking the values from different tables and displaying it. What should i do for that. I think i will post another question rather confussing you. right.
Code 'N' Weed