LEFT JOIN based on content of a field in matched rows
Generically, I'm attempting to take a table and join it with another table but only on rows that have a specific value in one of the fields.
Specifically, I have a users table and a contact table. The contact table ties a user to a piece of contact information (such as phone, email, or a myriad others). But I only want to pull in rows from the contact table that match a specific contact type (i.e. only pull phone records), but I still want to pull all of the records from the users table regardless of whether they have a phone record or not. This is in MariaDB.
Users table (simplified):
mysql> select * from users;
+----+-----------+----------+---------------------------------+
| id | firstname | lastname | title |
+----+-----------+----------+---------------------------------+
| 1 | Chris | Topher | Ordinary Citizen Extraordinaire |
| 2 | Howard | Roark | Solutions Architect |
| 3 | John | Snow | Knows Nothing |
+----+-----------+----------+---------------------------------+
Contact table:
mysql> select * from contact;
+----+--------+-------------+-------------------+
| id | userid | contacttype | contactdetails |
+----+--------+-------------+-------------------+
| 1 | 1 | email | one@two.Three |
| 2 | 1 | phone | 1-800-OHNOES1 |
| 3 | 2 | email | hroark@amazon.com |
+----+--------+-------------+-------------------+
What I'm trying to select is something like this:
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
| John | Snow | Knows Nothing | NULL |
+-----------+----------+---------------------------------+-------------------+
But my attempts with a left join result in too many or too few records:
mysql> select u.firstname,u.lastname,u.title,c.contactdetails from users u left join contact c on u.id=c.userid;
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Chris | Topher | Ordinary Citizen Extraordinaire | 1-800-OHNOES1 |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
| John | Snow | Knows Nothing | NULL |
+-----------+----------+---------------------------------+-------------------+
4 rows in set (0.00 sec)
mysql> select u.firstname,u.lastname,u.title,c.contactdetails from users u left
join contact c on u.id=c.userid WHERE c.contacttype='email';
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
+-----------+----------+---------------------------------+-------------------+
Is this possible in a single query? I'm mostly only familiar with the simple syntax of SQL, but I've tried EXCEPT clauses and subqueries to no avail. Redesigning the schema isn't out of the question, but it does have to account for the fact that users will have multiple forms of contact information (most have 4-5 emails/phones) to include contact types that we don't know about yet.
Thoughts?
join mariadb select
New contributor
Christopher Morrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
Generically, I'm attempting to take a table and join it with another table but only on rows that have a specific value in one of the fields.
Specifically, I have a users table and a contact table. The contact table ties a user to a piece of contact information (such as phone, email, or a myriad others). But I only want to pull in rows from the contact table that match a specific contact type (i.e. only pull phone records), but I still want to pull all of the records from the users table regardless of whether they have a phone record or not. This is in MariaDB.
Users table (simplified):
mysql> select * from users;
+----+-----------+----------+---------------------------------+
| id | firstname | lastname | title |
+----+-----------+----------+---------------------------------+
| 1 | Chris | Topher | Ordinary Citizen Extraordinaire |
| 2 | Howard | Roark | Solutions Architect |
| 3 | John | Snow | Knows Nothing |
+----+-----------+----------+---------------------------------+
Contact table:
mysql> select * from contact;
+----+--------+-------------+-------------------+
| id | userid | contacttype | contactdetails |
+----+--------+-------------+-------------------+
| 1 | 1 | email | one@two.Three |
| 2 | 1 | phone | 1-800-OHNOES1 |
| 3 | 2 | email | hroark@amazon.com |
+----+--------+-------------+-------------------+
What I'm trying to select is something like this:
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
| John | Snow | Knows Nothing | NULL |
+-----------+----------+---------------------------------+-------------------+
But my attempts with a left join result in too many or too few records:
mysql> select u.firstname,u.lastname,u.title,c.contactdetails from users u left join contact c on u.id=c.userid;
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Chris | Topher | Ordinary Citizen Extraordinaire | 1-800-OHNOES1 |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
| John | Snow | Knows Nothing | NULL |
+-----------+----------+---------------------------------+-------------------+
4 rows in set (0.00 sec)
mysql> select u.firstname,u.lastname,u.title,c.contactdetails from users u left
join contact c on u.id=c.userid WHERE c.contacttype='email';
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
+-----------+----------+---------------------------------+-------------------+
Is this possible in a single query? I'm mostly only familiar with the simple syntax of SQL, but I've tried EXCEPT clauses and subqueries to no avail. Redesigning the schema isn't out of the question, but it does have to account for the fact that users will have multiple forms of contact information (most have 4-5 emails/phones) to include contact types that we don't know about yet.
Thoughts?
join mariadb select
New contributor
Christopher Morrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
Generically, I'm attempting to take a table and join it with another table but only on rows that have a specific value in one of the fields.
Specifically, I have a users table and a contact table. The contact table ties a user to a piece of contact information (such as phone, email, or a myriad others). But I only want to pull in rows from the contact table that match a specific contact type (i.e. only pull phone records), but I still want to pull all of the records from the users table regardless of whether they have a phone record or not. This is in MariaDB.
Users table (simplified):
mysql> select * from users;
+----+-----------+----------+---------------------------------+
| id | firstname | lastname | title |
+----+-----------+----------+---------------------------------+
| 1 | Chris | Topher | Ordinary Citizen Extraordinaire |
| 2 | Howard | Roark | Solutions Architect |
| 3 | John | Snow | Knows Nothing |
+----+-----------+----------+---------------------------------+
Contact table:
mysql> select * from contact;
+----+--------+-------------+-------------------+
| id | userid | contacttype | contactdetails |
+----+--------+-------------+-------------------+
| 1 | 1 | email | one@two.Three |
| 2 | 1 | phone | 1-800-OHNOES1 |
| 3 | 2 | email | hroark@amazon.com |
+----+--------+-------------+-------------------+
What I'm trying to select is something like this:
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
| John | Snow | Knows Nothing | NULL |
+-----------+----------+---------------------------------+-------------------+
But my attempts with a left join result in too many or too few records:
mysql> select u.firstname,u.lastname,u.title,c.contactdetails from users u left join contact c on u.id=c.userid;
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Chris | Topher | Ordinary Citizen Extraordinaire | 1-800-OHNOES1 |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
| John | Snow | Knows Nothing | NULL |
+-----------+----------+---------------------------------+-------------------+
4 rows in set (0.00 sec)
mysql> select u.firstname,u.lastname,u.title,c.contactdetails from users u left
join contact c on u.id=c.userid WHERE c.contacttype='email';
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
+-----------+----------+---------------------------------+-------------------+
Is this possible in a single query? I'm mostly only familiar with the simple syntax of SQL, but I've tried EXCEPT clauses and subqueries to no avail. Redesigning the schema isn't out of the question, but it does have to account for the fact that users will have multiple forms of contact information (most have 4-5 emails/phones) to include contact types that we don't know about yet.
Thoughts?
join mariadb select
New contributor
Christopher Morrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Generically, I'm attempting to take a table and join it with another table but only on rows that have a specific value in one of the fields.
Specifically, I have a users table and a contact table. The contact table ties a user to a piece of contact information (such as phone, email, or a myriad others). But I only want to pull in rows from the contact table that match a specific contact type (i.e. only pull phone records), but I still want to pull all of the records from the users table regardless of whether they have a phone record or not. This is in MariaDB.
Users table (simplified):
mysql> select * from users;
+----+-----------+----------+---------------------------------+
| id | firstname | lastname | title |
+----+-----------+----------+---------------------------------+
| 1 | Chris | Topher | Ordinary Citizen Extraordinaire |
| 2 | Howard | Roark | Solutions Architect |
| 3 | John | Snow | Knows Nothing |
+----+-----------+----------+---------------------------------+
Contact table:
mysql> select * from contact;
+----+--------+-------------+-------------------+
| id | userid | contacttype | contactdetails |
+----+--------+-------------+-------------------+
| 1 | 1 | email | one@two.Three |
| 2 | 1 | phone | 1-800-OHNOES1 |
| 3 | 2 | email | hroark@amazon.com |
+----+--------+-------------+-------------------+
What I'm trying to select is something like this:
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
| John | Snow | Knows Nothing | NULL |
+-----------+----------+---------------------------------+-------------------+
But my attempts with a left join result in too many or too few records:
mysql> select u.firstname,u.lastname,u.title,c.contactdetails from users u left join contact c on u.id=c.userid;
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Chris | Topher | Ordinary Citizen Extraordinaire | 1-800-OHNOES1 |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
| John | Snow | Knows Nothing | NULL |
+-----------+----------+---------------------------------+-------------------+
4 rows in set (0.00 sec)
mysql> select u.firstname,u.lastname,u.title,c.contactdetails from users u left
join contact c on u.id=c.userid WHERE c.contacttype='email';
+-----------+----------+---------------------------------+-------------------+
| firstname | lastname | title | contactdetails |
+-----------+----------+---------------------------------+-------------------+
| Chris | Topher | Ordinary Citizen Extraordinaire | one@two.Three |
| Howard | Roark | Solutions Architect | hroark@amazon.com |
+-----------+----------+---------------------------------+-------------------+
Is this possible in a single query? I'm mostly only familiar with the simple syntax of SQL, but I've tried EXCEPT clauses and subqueries to no avail. Redesigning the schema isn't out of the question, but it does have to account for the fact that users will have multiple forms of contact information (most have 4-5 emails/phones) to include contact types that we don't know about yet.
Thoughts?
join mariadb select
join mariadb select
New contributor
Christopher Morrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Christopher Morrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Christopher Morrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 7 mins ago
Christopher MorrowChristopher Morrow
11
11
New contributor
Christopher Morrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Christopher Morrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Christopher Morrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Christopher Morrow is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f227726%2fleft-join-based-on-content-of-a-field-in-matched-rows%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Christopher Morrow is a new contributor. Be nice, and check out our Code of Conduct.
Christopher Morrow is a new contributor. Be nice, and check out our Code of Conduct.
Christopher Morrow is a new contributor. Be nice, and check out our Code of Conduct.
Christopher Morrow is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f227726%2fleft-join-based-on-content-of-a-field-in-matched-rows%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown