Guest User

Untitled

a guest
Aug 11th, 2018
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. SQL SELECT Columns From Multiple Tables Without Repeating Data
  2. SELECT Phone.Phone, Email.Email FROM Contacts.Phone, Contacts.Email
  3. WHERE Phone.ContactId = :contactId AND Email.ContactId = :contactId
  4.  
  5. Row 1: 555-555-5555 - email@email.com
  6. Row 2: 666-666-6666 - email@email.com
  7.  
  8. Row 1: 555-555-5555 - email@email.com
  9. Row 2: 666-666-6666 - NULL
  10.  
  11. SELECT 'EMAIL', Email.Email
  12. FROM Contacts.Email
  13. WHERE Email.ContactId = :contactId
  14. UNION ALL
  15. SELECT 'PHONE', Phone.Phone
  16. FROM Contacts.Phone
  17. WHERE Phone.ContactId = :contactId
  18. ORDER BY 1 -- put e-mail addresses before phone-numbers
  19. ;
  20.  
  21. SELECT Phone.Phone,
  22. CASE Email.Email
  23. WHEN @curEmail THEN NULL
  24. ELSE @curEmail := Email.Email END AS Email
  25. FROM Contacts.Phone, Contacts.Email, (SELECT @curEmail := '') AS r
  26. WHERE Phone.ContactId = :contactId AND Email.ContactId = :contactId
  27.  
  28. Row 1: 555-555-5555 - email@email.com
  29. Row 2: 666-666-6666 - NULL
  30.  
  31. SELECT
  32. Phone.Phone,
  33. Email.Email
  34. FROM
  35. Contacts.Phone
  36.  
  37. Left Outer Join Contacts.Email ON Phone.ContactId = Email.ContactId
  38.  
  39. Row 1: 555-555-5555 - email@email.com
  40. Row 2: 666-666-6666 - NULL
Add Comment
Please, Sign In to add comment