Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2012-01-04 : 14:32:11
|
| I have 1 table that is like this.Table UsersIDSPECIALIDSPECIALID2NAMETable Users Example Data1,5,6,Joe JonesTable SPECIALIDINFOUSERIDNAMEINFOTable SPECIALINFO Data1,COOL GUY5,SWEET GUYTable SPECIALIDINFO2USERIDNAMEINFOTable SPECIALINFO2 Data1,COOL GUY26,SWEET GUY2When i do my query from the Users Table, I dont want it to come back and say 1,5,6, Joe JonesI wanted to some how refence the ID used for SPECIALID, and get the value from teh SPECIALINFO table and get he name NAMEINFO FOR IT.so when i do a select * from users I would like to show like below1, SWEET GUY,SWEET GUY2, Joe JonesThanks again guys, Im new to SQL! :) |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-01-04 : 14:47:30
|
| [code]SELECT u.Id, s.NameInfo AS Info1, s2.NameInfo AS Info2, u.NAMEFROM [Users] u INNER JOIN SPECIALIDINFO s ON u.USERID= s.SPECIALID INNER JOIN SPECIALIDINFO2 s2 ON u.USERID= s2.SPECIALID2[/code]If you want to get the user id's for users who may not have data in both specialinfo tables use LEFT JOINs instead of INNER JOINs. |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2012-01-04 : 15:03:00
|
| Ok im confused now, i dont know why when im trying to get the info from the SPECIALID table, i use u.useridWould i use the ID like u.SPECIALID = s.SPECIALID ?*** EDIT ***TO make it simplerIf i have a table like thisNAMETABLEIDNAMSample Data1, COOL GUY2, SWEET GUYUSERTABLEIDCODE (This is ID From NAMETABLE)NAMESample Data1,1, Joe Jones2,2, Billy JonesAnd when i do a query i wanted it to say1, COOL GUY, Joe JonesHow would i get it to do that, i dont think im following how to do a simple join to get the Name of the ID, not the ID itself. |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2012-01-04 : 15:21:09
|
Ok here is the exact data, and name of columns.Applications Table Delegation Table Users Table My current query:select u.userid,u.username,d.applicationid,d.accountname,d.starttime,d.endtime,d.notimelimitfrom users u,delegation dwhere u.userid = useridThis gives me this result.I wanted to show all records for each person in the delegation table |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-01-04 : 17:30:21
|
I think the following would do that. Changes to your code are in red.SELECT u.userid, u.username, a.ApplicationName, d.accountname, d.starttime, d.endtime, d.notimelimitFROM users u, delegation d, applications aWHERE u.userid = userid AND a.ApplicationId = d.ApplicationId |
 |
|
|
|
|
|
|
|