Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. SELECT DISTINCT Person.ID, ...
  2. FROM Person LEFT JOIN Manager
  3. ON (Manager.ID = Person.ID OR Manager.ID = -1))
  4. WHERE (...)
  5.  
  6. id name
  7.  
  8. 1 Person One
  9. 2 Person Two
  10. 3 Person Three
  11. 4 Person Four
  12. 5 Person Five
  13.  
  14. id name
  15. -1 Admin
  16. 2 Manager One
  17. 3 Manager Two
  18.  
  19. SELECT DISTINCT *
  20. FROM Person LEFT JOIN Manager
  21. ON (Manager.id = Person.id OR Manager.id = -1)
  22.  
  23. Person One -1 Admin
  24. Person Two -1 Admin
  25. Person Two 2 Manager One
  26. Person Three -1 Admin
  27. Person Three 3 Manager Two
  28. Person Four -1 Admin
  29. Person Five -1 Admin
  30.  
  31. insert into #test (test1id, test)
  32. select 1, 'Judy'
  33. union all
  34. select 2, 'Sam'
  35. union all
  36. select 3, 'Nathan'
  37.  
  38. insert into #test2 (test2id, test1id, test2)
  39. select 1,1,'hello'
  40. union all
  41. select 2,1,'goodbye'
  42. union all
  43. select 3,2,'hello'
  44.  
  45. select * from #test t
  46. left join #test2 t2 on t.test1id = t2.test1id
  47. where test2 = 'goodbye'
  48. --result set
  49. --test1id test test2id test1id test2
  50. --1 Judy 2 1 goodbye
  51.  
  52. select * from #test t
  53. left join #test2 t2 on t.test1id = t2.test1id
  54. and test2 = 'goodbye'
  55. --result set
  56. --test1id test test2id test1id test2
  57. --1 Judy 2 1 goodbye
  58. --2 Sam NULL NULL NULL
  59. --3 Nathan NULL NULL NULL
  60.  
  61. select * from #test t
  62. left join #test2 t2 on t.test1id = t2.test1id
  63. where test2id is null
  64. --result set
  65. --test1id test test2id test1id test2
  66. --3 Nathan NULL NULL NULL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement