Author |
Topic |
digit
Starting Member
1 Post |
Posted - 2014-07-03 : 21:41:22
|
SELECT Users.ID, Users.Name, Users.Address, Users.Phone, Users.Email, Users.Password, Dogs.DID, Dogs.DogAge, Dogs.DogBreed, Dogs.DogName, Papers.PID, Papers.URLtoPaperFROM UsersINNER JOIN DogsON Users.ID=Dogs.IDINNER JOIN PapersON Users.ID=Papers.IDWHERE Users.ID = 11 David 14623 995 qwill@outlook.com *** 1 2 Rottweiler Barney 1 papers.html1 David 14623 995 qwill@outlook.com *** 2 3 Sharpei Wondering 1 papers.htmlOk, As you can see the USER part of these two returned rows are the same.I want this to be ONE row with the Dogs.DID, Dogs.DogAge, Dogs.DogBreed, Dogs.DogName ordered next to each other from first to last.In this example, the desired effect is to be1 David 14623 995 qwill@outlook.com *** 1 2 Rottweiler Barney 2 3 Sharpei Wondering 1 papers.htmlhelp? |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-07-04 : 01:07:17
|
[code];With UsersAS( SELECT 1 as ID , 'David' as Name,'14623' as Address, '995'as Phone,'qwill@outlook.com' as Email,'***' as Password),DogsAS (SELECT 1 as ID, 1 as DID,2 as DogAge, 'Rottweiler' AS DogBreed, 'Barney' AS DogName UNION ALL SELECT 1 as ID,2,3,'Sharpei','Wondering'),PapersAS (SELECt 1 as ID, 1 as PID , 'papers.html' as URLtoPaper)SELECT Users.ID, Users.Name, Users.Address, Users.Phone, Users.Email, Users.Password, Dogs.DogsSTR, Papers.PID, Papers.URLtoPaperFROM UsersCROSS APPLY( SELECT ' ' + CAST(DID as VARCHAR(30)) + ' ' + CAST(DogAge AS VARCHAR(30)) + ' ' + CAST(DogBreed AS VARCHAR(30)) + ' ' + CAST(DogName AS VARCHAR(30)) FROM Dogs WHERE Dogs.ID = Users.ID FOR XML PATH('') )AS Dogs(DogsSTR)INNER JOIN PapersON Users.ID=Papers.IDWHERE Users.ID = 1[/code]output:[code]ID Name Address Phone Email Password DogsSTR PID URLtoPaper1 David 14623 995 qwill@outlook.com *** 1 2 Rottweiler Barney 2 3 Sharpei Wondering 1 papers.html[/code]sabinWeb MCP |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-07-04 : 01:09:43
|
You can add a , SELECT Users.ID, Users.Name, Users.Address, Users.Phone, Users.Email, Users.Password, STUFF(Dogs.DogsSTR,1,1,'') as DogsSTR, Papers.PID, Papers.URLtoPaperFROM UsersCROSS APPLY( SELECT ', ' + CAST(DID as VARCHAR(30)) + ' ' + CAST(DogAge AS VARCHAR(30)) + ' ' + CAST(DogBreed AS VARCHAR(30)) + ' ' + CAST(DogName AS VARCHAR(30)) FROM Dogs WHERE Dogs.ID = Users.ID FOR XML PATH('') )AS Dogs(DogsSTR)INNER JOIN PapersON Users.ID=Papers.IDWHERE Users.ID = 1 [/code]ID Name Address Phone Email Password DogsSTR PID URLtoPaper1 David 14623 995 qwill@outlook.com *** 1 2 Rottweiler Barney, 2 3 Sharpei Wondering 1 papers.html[/code]sabinWeb MCP |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-07-04 : 01:12:20
|
output:ID Name Address Phone Email Password DogsSTR PID URLtoPaper1 David 14623 995 qwill@outlook.com *** 1 2 Rottweiler Barney, 2 3 Sharpei Wondering 1 papers.html sabinWeb MCP |
|
|
|
|
|