| Author |
Topic |
|
cutepraba
Yak Posting Veteran
53 Posts |
Posted - 2006-09-11 : 03:24:22
|
I have two tables namedTable 1Id name Role23 John student24 Peter teacher25 james AdminTable 2job tid sid aid remarks2345 23 24 25 Three membersNow how to take the names in select command I need to display their names likeJob tname sname remarks 2345 teacher name student name remark____________Praba |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-11 : 03:39:30
|
| [code]Select t1.Job,T2.TeachName,T3.StudentName,T4.AdminName,T1.Remarks From Table2 T1Inner Join (Select [ID],Name As TeachName From Table1 Where Role = 'teacher') As T2On T1.[Tid] = T2.[ID](Select [ID],Name As StudentName From Table1 Where Role = 'student') As T3On T1.[Sid] = T3.[ID](Select [ID],Name As AdminName From Table1 Where Role = 'Admin') As T4On T1.[aid] = T4.[ID][/code]Chirag |
 |
|
|
cutepraba
Yak Posting Veteran
53 Posts |
Posted - 2006-09-11 : 06:28:13
|
| I have another 37 columns of data after "Remarks column" in Table2Can we use t1.* instead of t1.c1, t1.c2, t1.c3............____________Praba |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-11 : 06:30:30
|
| yes you can do that...Chirag |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-11 : 06:30:39
|
| What happened (which result did you get) when you tried, before asking here?Peter LarssonHelsingborg, Sweden |
 |
|
|
cutepraba
Yak Posting Veteran
53 Posts |
Posted - 2006-09-11 : 06:51:51
|
| cname means clerknamestaff table containing staff detailsi am getting error messageIncorrect syntax near the keyword 'select'My queryselect Q.*,t1.tname, t2.sname, t3.aname, t4.cname from Drama q Inner Join (Select staffid, staffname As tName From staff) As t1 on q.[tid] = t1.[staffID] (Select staffid, staffname As prName From staff) As t2 on q.[sid] = t2.[staffID] (Select staffid, staffname As qcName From staff) As t3 on q.[aid] = t3.[staffID] (Select staffid, staffname As fmName From staff) As t4 on q.[cid] = t4.[staffID]Pleaseeeeeeeeeeeeeeeeeeeeee help me____________Praba |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-11 : 07:23:08
|
Where did all the JOINs go?select Q.*, t1.tname, t2.sname, t3.aname, t4.cnamefrom Drama qInner Join ( Select staffid, staffname As tName From staff ) As t1 on q.[tid] = t1.[staffID]inner join ( Select staffid, staffname As prName From staff ) As t2 on q.[sid] = t2.[staffID]inner join ( Select staffid, staffname As qcName From staff ) As t3 on q.[aid] = t3.[staffID]inner join ( Select staffid, staffname As fmName From staff ) As t4 on q.[cid] = t4.[staffID] Peter LarssonHelsingborg, Sweden |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-11 : 07:25:43
|
Praba, you will not get desired output from the query which you have wrote... there should be where cluase... select Q.*, t1.tname, t2.sname, t3.aname, t4.cnamefrom Drama qInner Join ( Select staffid, staffname As tName From staff Where Role = <Some Role> ) As t1 on q.[tid] = t1.[staffID]inner join ( Select staffid, staffname As prName From staff Where Role = <Some Role> ) As t2 on q.[sid] = t2.[staffID]inner join ( Select staffid, staffname As qcName From staff Where Role = <Some Role> ) As t3 on q.[aid] = t3.[staffID]inner join ( Select staffid, staffname As fmName From staff Where Role = <Some Role> ) As t4 on q.[cid] = t4.[staffID] Chirag |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-11 : 07:25:45
|
| If there is any change that either tid, sid, aid or cid can be NULL, use LEFT JOIN instead.Peter LarssonHelsingborg, Sweden |
 |
|
|
cutepraba
Yak Posting Veteran
53 Posts |
Posted - 2006-09-11 : 09:35:39
|
| How to set this topic as "Solved"____________Praba |
 |
|
|
cutepraba
Yak Posting Veteran
53 Posts |
Posted - 2006-09-11 : 09:36:45
|
Thank you to all my Seniors ____________Praba |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-11 : 09:42:17
|
| Edit the TOPIC name in the "General SQL Server" section.Add [SOLVED] to the heading.It would be nice to know what did help you.Peter LarssonHelsingborg, Sweden |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-11 : 09:44:24
|
| of course it will be your query who helps a lots of people ;)Chirag |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-09-11 : 12:04:13
|
| If your table1 has hundreds of roles, you may have problem in displaying the way you want MadhivananFailing to plan is Planning to fail |
 |
|
|
|