Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SQL SELECT Columns From Multiple Tables Without Repeating Data
- SELECT Phone.Phone, Email.Email FROM Contacts.Phone, Contacts.Email
- WHERE Phone.ContactId = :contactId AND Email.ContactId = :contactId
- Row 1: 555-555-5555 - email@email.com
- Row 2: 666-666-6666 - email@email.com
- Row 1: 555-555-5555 - email@email.com
- Row 2: 666-666-6666 - NULL
- SELECT 'EMAIL', Email.Email
- FROM Contacts.Email
- WHERE Email.ContactId = :contactId
- UNION ALL
- SELECT 'PHONE', Phone.Phone
- FROM Contacts.Phone
- WHERE Phone.ContactId = :contactId
- ORDER BY 1 -- put e-mail addresses before phone-numbers
- ;
- SELECT Phone.Phone,
- CASE Email.Email
- WHEN @curEmail THEN NULL
- ELSE @curEmail := Email.Email END AS Email
- FROM Contacts.Phone, Contacts.Email, (SELECT @curEmail := '') AS r
- WHERE Phone.ContactId = :contactId AND Email.ContactId = :contactId
- Row 1: 555-555-5555 - email@email.com
- Row 2: 666-666-6666 - NULL
- SELECT
- Phone.Phone,
- Email.Email
- FROM
- Contacts.Phone
- Left Outer Join Contacts.Email ON Phone.ContactId = Email.ContactId
- Row 1: 555-555-5555 - email@email.com
- Row 2: 666-666-6666 - NULL
Add Comment
Please, Sign In to add comment