How to find items with multiple different names
I'm having trouble with a very simple sql query. I want to identify all items that have more than one name. Here's what I'm currently doing:
select group_concat(distinct name) names
from table
group by master_id
having names like '%,%'
Unfortunately, a lot of names
have a ,
in it, so the above doesn't work well. What would be the correct way to do this query?
mysql sql
add a comment |
I'm having trouble with a very simple sql query. I want to identify all items that have more than one name. Here's what I'm currently doing:
select group_concat(distinct name) names
from table
group by master_id
having names like '%,%'
Unfortunately, a lot of names
have a ,
in it, so the above doesn't work well. What would be the correct way to do this query?
mysql sql
Please add sample data to your question.
– Tim Biegeleisen
Jan 1 at 4:00
What does 'doesn't work well' exactly mean?
– Quasimodo's clone
Jan 1 at 4:12
@Quasimodo'sclone It returns rows which correspond to a single name containing a comma.
– duskwuff
Jan 1 at 4:14
Then there's already a correct answer+comment if you are looking for more than one distinct names having the same master_id.
– Quasimodo's clone
Jan 1 at 4:17
@TimBiegeleisen I think you've given the correct answer in your comment below, just doing thehaving count(distinct name) > 1
, if you want to put that in an answer I'll go ahead and accept it.
– David L
Jan 1 at 4:41
add a comment |
I'm having trouble with a very simple sql query. I want to identify all items that have more than one name. Here's what I'm currently doing:
select group_concat(distinct name) names
from table
group by master_id
having names like '%,%'
Unfortunately, a lot of names
have a ,
in it, so the above doesn't work well. What would be the correct way to do this query?
mysql sql
I'm having trouble with a very simple sql query. I want to identify all items that have more than one name. Here's what I'm currently doing:
select group_concat(distinct name) names
from table
group by master_id
having names like '%,%'
Unfortunately, a lot of names
have a ,
in it, so the above doesn't work well. What would be the correct way to do this query?
mysql sql
mysql sql
edited Jan 1 at 4:47
Tim Biegeleisen
226k1392145
226k1392145
asked Jan 1 at 3:53
David LDavid L
25916
25916
Please add sample data to your question.
– Tim Biegeleisen
Jan 1 at 4:00
What does 'doesn't work well' exactly mean?
– Quasimodo's clone
Jan 1 at 4:12
@Quasimodo'sclone It returns rows which correspond to a single name containing a comma.
– duskwuff
Jan 1 at 4:14
Then there's already a correct answer+comment if you are looking for more than one distinct names having the same master_id.
– Quasimodo's clone
Jan 1 at 4:17
@TimBiegeleisen I think you've given the correct answer in your comment below, just doing thehaving count(distinct name) > 1
, if you want to put that in an answer I'll go ahead and accept it.
– David L
Jan 1 at 4:41
add a comment |
Please add sample data to your question.
– Tim Biegeleisen
Jan 1 at 4:00
What does 'doesn't work well' exactly mean?
– Quasimodo's clone
Jan 1 at 4:12
@Quasimodo'sclone It returns rows which correspond to a single name containing a comma.
– duskwuff
Jan 1 at 4:14
Then there's already a correct answer+comment if you are looking for more than one distinct names having the same master_id.
– Quasimodo's clone
Jan 1 at 4:17
@TimBiegeleisen I think you've given the correct answer in your comment below, just doing thehaving count(distinct name) > 1
, if you want to put that in an answer I'll go ahead and accept it.
– David L
Jan 1 at 4:41
Please add sample data to your question.
– Tim Biegeleisen
Jan 1 at 4:00
Please add sample data to your question.
– Tim Biegeleisen
Jan 1 at 4:00
What does 'doesn't work well' exactly mean?
– Quasimodo's clone
Jan 1 at 4:12
What does 'doesn't work well' exactly mean?
– Quasimodo's clone
Jan 1 at 4:12
@Quasimodo'sclone It returns rows which correspond to a single name containing a comma.
– duskwuff
Jan 1 at 4:14
@Quasimodo'sclone It returns rows which correspond to a single name containing a comma.
– duskwuff
Jan 1 at 4:14
Then there's already a correct answer+comment if you are looking for more than one distinct names having the same master_id.
– Quasimodo's clone
Jan 1 at 4:17
Then there's already a correct answer+comment if you are looking for more than one distinct names having the same master_id.
– Quasimodo's clone
Jan 1 at 4:17
@TimBiegeleisen I think you've given the correct answer in your comment below, just doing the
having count(distinct name) > 1
, if you want to put that in an answer I'll go ahead and accept it.– David L
Jan 1 at 4:41
@TimBiegeleisen I think you've given the correct answer in your comment below, just doing the
having count(distinct name) > 1
, if you want to put that in an answer I'll go ahead and accept it.– David L
Jan 1 at 4:41
add a comment |
2 Answers
2
active
oldest
votes
Here is a correct version of your query:
SELECT
master_id,
GROUP_CONCAT(DISTINCT name) names
FROM yourTable
GROUP BY master_id
HAVING COUNT(DISTINCT name) > 1;
The reason we need to count distinct in the HAVING
clause is that a logical item in the aggregated string is a distinct name.
looks good, thank you for the solution.
– David L
Jan 1 at 4:46
add a comment |
The correct solution would be:
… HAVING COUNT(name) > 1
In a query using GROUP BY
, aggregate functions like COUNT()
, MIN()
, and MAX()
(as well as GROUP_CONCAT()
, as well as a few others) can be used to operate on all values of a column in the grouped rows.
You could also include COUNT(name)
in the columns to return the number of names for the master_id
.
2
It should beHAVING COUNT(DISTINCT name)
, but let's see the data the OP might add to the question.
– Tim Biegeleisen
Jan 1 at 4:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53992907%2fhow-to-find-items-with-multiple-different-names%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a correct version of your query:
SELECT
master_id,
GROUP_CONCAT(DISTINCT name) names
FROM yourTable
GROUP BY master_id
HAVING COUNT(DISTINCT name) > 1;
The reason we need to count distinct in the HAVING
clause is that a logical item in the aggregated string is a distinct name.
looks good, thank you for the solution.
– David L
Jan 1 at 4:46
add a comment |
Here is a correct version of your query:
SELECT
master_id,
GROUP_CONCAT(DISTINCT name) names
FROM yourTable
GROUP BY master_id
HAVING COUNT(DISTINCT name) > 1;
The reason we need to count distinct in the HAVING
clause is that a logical item in the aggregated string is a distinct name.
looks good, thank you for the solution.
– David L
Jan 1 at 4:46
add a comment |
Here is a correct version of your query:
SELECT
master_id,
GROUP_CONCAT(DISTINCT name) names
FROM yourTable
GROUP BY master_id
HAVING COUNT(DISTINCT name) > 1;
The reason we need to count distinct in the HAVING
clause is that a logical item in the aggregated string is a distinct name.
Here is a correct version of your query:
SELECT
master_id,
GROUP_CONCAT(DISTINCT name) names
FROM yourTable
GROUP BY master_id
HAVING COUNT(DISTINCT name) > 1;
The reason we need to count distinct in the HAVING
clause is that a logical item in the aggregated string is a distinct name.
edited Jan 1 at 4:47
answered Jan 1 at 4:46
Tim BiegeleisenTim Biegeleisen
226k1392145
226k1392145
looks good, thank you for the solution.
– David L
Jan 1 at 4:46
add a comment |
looks good, thank you for the solution.
– David L
Jan 1 at 4:46
looks good, thank you for the solution.
– David L
Jan 1 at 4:46
looks good, thank you for the solution.
– David L
Jan 1 at 4:46
add a comment |
The correct solution would be:
… HAVING COUNT(name) > 1
In a query using GROUP BY
, aggregate functions like COUNT()
, MIN()
, and MAX()
(as well as GROUP_CONCAT()
, as well as a few others) can be used to operate on all values of a column in the grouped rows.
You could also include COUNT(name)
in the columns to return the number of names for the master_id
.
2
It should beHAVING COUNT(DISTINCT name)
, but let's see the data the OP might add to the question.
– Tim Biegeleisen
Jan 1 at 4:01
add a comment |
The correct solution would be:
… HAVING COUNT(name) > 1
In a query using GROUP BY
, aggregate functions like COUNT()
, MIN()
, and MAX()
(as well as GROUP_CONCAT()
, as well as a few others) can be used to operate on all values of a column in the grouped rows.
You could also include COUNT(name)
in the columns to return the number of names for the master_id
.
2
It should beHAVING COUNT(DISTINCT name)
, but let's see the data the OP might add to the question.
– Tim Biegeleisen
Jan 1 at 4:01
add a comment |
The correct solution would be:
… HAVING COUNT(name) > 1
In a query using GROUP BY
, aggregate functions like COUNT()
, MIN()
, and MAX()
(as well as GROUP_CONCAT()
, as well as a few others) can be used to operate on all values of a column in the grouped rows.
You could also include COUNT(name)
in the columns to return the number of names for the master_id
.
The correct solution would be:
… HAVING COUNT(name) > 1
In a query using GROUP BY
, aggregate functions like COUNT()
, MIN()
, and MAX()
(as well as GROUP_CONCAT()
, as well as a few others) can be used to operate on all values of a column in the grouped rows.
You could also include COUNT(name)
in the columns to return the number of names for the master_id
.
answered Jan 1 at 3:57
duskwuffduskwuff
148k19177234
148k19177234
2
It should beHAVING COUNT(DISTINCT name)
, but let's see the data the OP might add to the question.
– Tim Biegeleisen
Jan 1 at 4:01
add a comment |
2
It should beHAVING COUNT(DISTINCT name)
, but let's see the data the OP might add to the question.
– Tim Biegeleisen
Jan 1 at 4:01
2
2
It should be
HAVING COUNT(DISTINCT name)
, but let's see the data the OP might add to the question.– Tim Biegeleisen
Jan 1 at 4:01
It should be
HAVING COUNT(DISTINCT name)
, but let's see the data the OP might add to the question.– Tim Biegeleisen
Jan 1 at 4:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53992907%2fhow-to-find-items-with-multiple-different-names%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
Please add sample data to your question.
– Tim Biegeleisen
Jan 1 at 4:00
What does 'doesn't work well' exactly mean?
– Quasimodo's clone
Jan 1 at 4:12
@Quasimodo'sclone It returns rows which correspond to a single name containing a comma.
– duskwuff
Jan 1 at 4:14
Then there's already a correct answer+comment if you are looking for more than one distinct names having the same master_id.
– Quasimodo's clone
Jan 1 at 4:17
@TimBiegeleisen I think you've given the correct answer in your comment below, just doing the
having count(distinct name) > 1
, if you want to put that in an answer I'll go ahead and accept it.– David L
Jan 1 at 4:41