How to implement EVERY and NO operations for a joined 'list' table
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have two tables Person and _Person_Name which contains a column Name and a column Owner with Person.Id as foreign key. I'm looking for two search operations I'd call EVERY and NO.
1.) Every
The following returns only the Person IDs for which all corresponding names match for query LIKE '%n%'
:
SELECT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE __T1.Name LIKE '%n%'
GROUP BY Result
HAVING Count(Result)=(Select Count(*) FROM _Person_Name
WHERE Person.Id=_Person_Name.Owner)
But the problem is that I also have to deal with other queries for which just a single match suffices, and the HAVING clause applies to all terms in the WHERE clause.
Is there a way to get the same effect as in the HAVING clause in the above query, but somehow express this within the WHERE clause such that other, ordinary conditions can be added to it?
Example:
SELECT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE (EVERY __T1.Name LIKE '%n%')
OR (__T1.Name LIKE 'John')
The second disjunct should just behave in the ordinary way without any restrictions. Now I'm looking for a way to express EVERY like in the above HAVING clause. The query should return __T1.Owner
(=Person.Id) whenever it is the case that one field with matching owner has a name field __T1.Name LIKE 'John'
or it is the case that all __T1.Name
fields with matching owner contain an 'n'.
If it is not possible to express this in the WHERE clause, how can it be expressed?
2.) No
The NO search operation is just like EVERY but the base condition is negated, i.e., I'm looking for the Persons for which none of their associated Name parts matches the query. I suppose I can get that easily if I have a solution for EVERY.
EDIT: The answer by Dávid Laczkó is correct. If he hasn't edited his reply already, a small change is required. Here is the solution for NO based on his answer:
SELECT DISTINCT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2
WHERE __T2.Name LIKE '%n%' AND Person.Id=__T2.Owner
)
sql database sqlite sqlite3
add a comment |
I have two tables Person and _Person_Name which contains a column Name and a column Owner with Person.Id as foreign key. I'm looking for two search operations I'd call EVERY and NO.
1.) Every
The following returns only the Person IDs for which all corresponding names match for query LIKE '%n%'
:
SELECT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE __T1.Name LIKE '%n%'
GROUP BY Result
HAVING Count(Result)=(Select Count(*) FROM _Person_Name
WHERE Person.Id=_Person_Name.Owner)
But the problem is that I also have to deal with other queries for which just a single match suffices, and the HAVING clause applies to all terms in the WHERE clause.
Is there a way to get the same effect as in the HAVING clause in the above query, but somehow express this within the WHERE clause such that other, ordinary conditions can be added to it?
Example:
SELECT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE (EVERY __T1.Name LIKE '%n%')
OR (__T1.Name LIKE 'John')
The second disjunct should just behave in the ordinary way without any restrictions. Now I'm looking for a way to express EVERY like in the above HAVING clause. The query should return __T1.Owner
(=Person.Id) whenever it is the case that one field with matching owner has a name field __T1.Name LIKE 'John'
or it is the case that all __T1.Name
fields with matching owner contain an 'n'.
If it is not possible to express this in the WHERE clause, how can it be expressed?
2.) No
The NO search operation is just like EVERY but the base condition is negated, i.e., I'm looking for the Persons for which none of their associated Name parts matches the query. I suppose I can get that easily if I have a solution for EVERY.
EDIT: The answer by Dávid Laczkó is correct. If he hasn't edited his reply already, a small change is required. Here is the solution for NO based on his answer:
SELECT DISTINCT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2
WHERE __T2.Name LIKE '%n%' AND Person.Id=__T2.Owner
)
sql database sqlite sqlite3
add a comment |
I have two tables Person and _Person_Name which contains a column Name and a column Owner with Person.Id as foreign key. I'm looking for two search operations I'd call EVERY and NO.
1.) Every
The following returns only the Person IDs for which all corresponding names match for query LIKE '%n%'
:
SELECT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE __T1.Name LIKE '%n%'
GROUP BY Result
HAVING Count(Result)=(Select Count(*) FROM _Person_Name
WHERE Person.Id=_Person_Name.Owner)
But the problem is that I also have to deal with other queries for which just a single match suffices, and the HAVING clause applies to all terms in the WHERE clause.
Is there a way to get the same effect as in the HAVING clause in the above query, but somehow express this within the WHERE clause such that other, ordinary conditions can be added to it?
Example:
SELECT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE (EVERY __T1.Name LIKE '%n%')
OR (__T1.Name LIKE 'John')
The second disjunct should just behave in the ordinary way without any restrictions. Now I'm looking for a way to express EVERY like in the above HAVING clause. The query should return __T1.Owner
(=Person.Id) whenever it is the case that one field with matching owner has a name field __T1.Name LIKE 'John'
or it is the case that all __T1.Name
fields with matching owner contain an 'n'.
If it is not possible to express this in the WHERE clause, how can it be expressed?
2.) No
The NO search operation is just like EVERY but the base condition is negated, i.e., I'm looking for the Persons for which none of their associated Name parts matches the query. I suppose I can get that easily if I have a solution for EVERY.
EDIT: The answer by Dávid Laczkó is correct. If he hasn't edited his reply already, a small change is required. Here is the solution for NO based on his answer:
SELECT DISTINCT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2
WHERE __T2.Name LIKE '%n%' AND Person.Id=__T2.Owner
)
sql database sqlite sqlite3
I have two tables Person and _Person_Name which contains a column Name and a column Owner with Person.Id as foreign key. I'm looking for two search operations I'd call EVERY and NO.
1.) Every
The following returns only the Person IDs for which all corresponding names match for query LIKE '%n%'
:
SELECT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE __T1.Name LIKE '%n%'
GROUP BY Result
HAVING Count(Result)=(Select Count(*) FROM _Person_Name
WHERE Person.Id=_Person_Name.Owner)
But the problem is that I also have to deal with other queries for which just a single match suffices, and the HAVING clause applies to all terms in the WHERE clause.
Is there a way to get the same effect as in the HAVING clause in the above query, but somehow express this within the WHERE clause such that other, ordinary conditions can be added to it?
Example:
SELECT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE (EVERY __T1.Name LIKE '%n%')
OR (__T1.Name LIKE 'John')
The second disjunct should just behave in the ordinary way without any restrictions. Now I'm looking for a way to express EVERY like in the above HAVING clause. The query should return __T1.Owner
(=Person.Id) whenever it is the case that one field with matching owner has a name field __T1.Name LIKE 'John'
or it is the case that all __T1.Name
fields with matching owner contain an 'n'.
If it is not possible to express this in the WHERE clause, how can it be expressed?
2.) No
The NO search operation is just like EVERY but the base condition is negated, i.e., I'm looking for the Persons for which none of their associated Name parts matches the query. I suppose I can get that easily if I have a solution for EVERY.
EDIT: The answer by Dávid Laczkó is correct. If he hasn't edited his reply already, a small change is required. Here is the solution for NO based on his answer:
SELECT DISTINCT Person.Id as Result FROM Person
INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2
WHERE __T2.Name LIKE '%n%' AND Person.Id=__T2.Owner
)
sql database sqlite sqlite3
sql database sqlite sqlite3
edited Nov 17 '18 at 15:05
Eric '3ToedSloth'
asked Nov 16 '18 at 17:43
Eric '3ToedSloth'Eric '3ToedSloth'
1947
1947
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Let me start with an idea for NO: you can check if no row exists if you look up by condition LIKE '%n%'. You can do it in the WHERE clause with EXISTS subquery:
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2 WHERE Person.Id = __T2.Owner
and __T2.Name LIKE '%n%'
)
OR (__T1.Name LIKE 'John')
NOT EXISTS checks if there is NO match.
Based on this the implementation for EVERY is to see if the negation of your condition for no row is true, so you are looking for NOT LIKE '%n%':
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2 WHERE Person.Id = __T2.Owner
and __T2.Name NOT LIKE '%n%'
)
OR (__T1.Name LIKE 'John')
This time NOT EXISTS will make sure no rows found that does NOT match your criteria - therefore EVERY row matched it.
Perfect! This works, with a small change. The inner SELECT would need a JOIN for the ON condition to work, instead I used: SELECT DISTINCT Person.Id as Result FROM Person INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner WHERE NOT EXISTS ( SELECT 1 FROM _Person_Name AS __T2 WHERE __T2.Name LIKE '%n%' AND Person.Id=__T2.Owner )
– Eric '3ToedSloth'
Nov 17 '18 at 15:01
Oh, I did it incorrectly, but you don't need a join there. Try my update.
– Dávid Laczkó
Nov 17 '18 at 15:15
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%2f53342896%2fhow-to-implement-every-and-no-operations-for-a-joined-list-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let me start with an idea for NO: you can check if no row exists if you look up by condition LIKE '%n%'. You can do it in the WHERE clause with EXISTS subquery:
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2 WHERE Person.Id = __T2.Owner
and __T2.Name LIKE '%n%'
)
OR (__T1.Name LIKE 'John')
NOT EXISTS checks if there is NO match.
Based on this the implementation for EVERY is to see if the negation of your condition for no row is true, so you are looking for NOT LIKE '%n%':
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2 WHERE Person.Id = __T2.Owner
and __T2.Name NOT LIKE '%n%'
)
OR (__T1.Name LIKE 'John')
This time NOT EXISTS will make sure no rows found that does NOT match your criteria - therefore EVERY row matched it.
Perfect! This works, with a small change. The inner SELECT would need a JOIN for the ON condition to work, instead I used: SELECT DISTINCT Person.Id as Result FROM Person INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner WHERE NOT EXISTS ( SELECT 1 FROM _Person_Name AS __T2 WHERE __T2.Name LIKE '%n%' AND Person.Id=__T2.Owner )
– Eric '3ToedSloth'
Nov 17 '18 at 15:01
Oh, I did it incorrectly, but you don't need a join there. Try my update.
– Dávid Laczkó
Nov 17 '18 at 15:15
add a comment |
Let me start with an idea for NO: you can check if no row exists if you look up by condition LIKE '%n%'. You can do it in the WHERE clause with EXISTS subquery:
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2 WHERE Person.Id = __T2.Owner
and __T2.Name LIKE '%n%'
)
OR (__T1.Name LIKE 'John')
NOT EXISTS checks if there is NO match.
Based on this the implementation for EVERY is to see if the negation of your condition for no row is true, so you are looking for NOT LIKE '%n%':
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2 WHERE Person.Id = __T2.Owner
and __T2.Name NOT LIKE '%n%'
)
OR (__T1.Name LIKE 'John')
This time NOT EXISTS will make sure no rows found that does NOT match your criteria - therefore EVERY row matched it.
Perfect! This works, with a small change. The inner SELECT would need a JOIN for the ON condition to work, instead I used: SELECT DISTINCT Person.Id as Result FROM Person INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner WHERE NOT EXISTS ( SELECT 1 FROM _Person_Name AS __T2 WHERE __T2.Name LIKE '%n%' AND Person.Id=__T2.Owner )
– Eric '3ToedSloth'
Nov 17 '18 at 15:01
Oh, I did it incorrectly, but you don't need a join there. Try my update.
– Dávid Laczkó
Nov 17 '18 at 15:15
add a comment |
Let me start with an idea for NO: you can check if no row exists if you look up by condition LIKE '%n%'. You can do it in the WHERE clause with EXISTS subquery:
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2 WHERE Person.Id = __T2.Owner
and __T2.Name LIKE '%n%'
)
OR (__T1.Name LIKE 'John')
NOT EXISTS checks if there is NO match.
Based on this the implementation for EVERY is to see if the negation of your condition for no row is true, so you are looking for NOT LIKE '%n%':
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2 WHERE Person.Id = __T2.Owner
and __T2.Name NOT LIKE '%n%'
)
OR (__T1.Name LIKE 'John')
This time NOT EXISTS will make sure no rows found that does NOT match your criteria - therefore EVERY row matched it.
Let me start with an idea for NO: you can check if no row exists if you look up by condition LIKE '%n%'. You can do it in the WHERE clause with EXISTS subquery:
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2 WHERE Person.Id = __T2.Owner
and __T2.Name LIKE '%n%'
)
OR (__T1.Name LIKE 'John')
NOT EXISTS checks if there is NO match.
Based on this the implementation for EVERY is to see if the negation of your condition for no row is true, so you are looking for NOT LIKE '%n%':
WHERE
NOT EXISTS (
SELECT 1 FROM _Person_Name AS __T2 WHERE Person.Id = __T2.Owner
and __T2.Name NOT LIKE '%n%'
)
OR (__T1.Name LIKE 'John')
This time NOT EXISTS will make sure no rows found that does NOT match your criteria - therefore EVERY row matched it.
edited Nov 17 '18 at 15:14
answered Nov 16 '18 at 18:19
Dávid LaczkóDávid Laczkó
419129
419129
Perfect! This works, with a small change. The inner SELECT would need a JOIN for the ON condition to work, instead I used: SELECT DISTINCT Person.Id as Result FROM Person INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner WHERE NOT EXISTS ( SELECT 1 FROM _Person_Name AS __T2 WHERE __T2.Name LIKE '%n%' AND Person.Id=__T2.Owner )
– Eric '3ToedSloth'
Nov 17 '18 at 15:01
Oh, I did it incorrectly, but you don't need a join there. Try my update.
– Dávid Laczkó
Nov 17 '18 at 15:15
add a comment |
Perfect! This works, with a small change. The inner SELECT would need a JOIN for the ON condition to work, instead I used: SELECT DISTINCT Person.Id as Result FROM Person INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner WHERE NOT EXISTS ( SELECT 1 FROM _Person_Name AS __T2 WHERE __T2.Name LIKE '%n%' AND Person.Id=__T2.Owner )
– Eric '3ToedSloth'
Nov 17 '18 at 15:01
Oh, I did it incorrectly, but you don't need a join there. Try my update.
– Dávid Laczkó
Nov 17 '18 at 15:15
Perfect! This works, with a small change. The inner SELECT would need a JOIN for the ON condition to work, instead I used: SELECT DISTINCT Person.Id as Result FROM Person INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner WHERE NOT EXISTS ( SELECT 1 FROM _Person_Name AS __T2 WHERE __T2.Name LIKE '%n%' AND Person.Id=__T2.Owner )
– Eric '3ToedSloth'
Nov 17 '18 at 15:01
Perfect! This works, with a small change. The inner SELECT would need a JOIN for the ON condition to work, instead I used: SELECT DISTINCT Person.Id as Result FROM Person INNER JOIN _Person_Name AS __T1 ON Person.Id = __T1.Owner WHERE NOT EXISTS ( SELECT 1 FROM _Person_Name AS __T2 WHERE __T2.Name LIKE '%n%' AND Person.Id=__T2.Owner )
– Eric '3ToedSloth'
Nov 17 '18 at 15:01
Oh, I did it incorrectly, but you don't need a join there. Try my update.
– Dávid Laczkó
Nov 17 '18 at 15:15
Oh, I did it incorrectly, but you don't need a join there. Try my update.
– Dávid Laczkó
Nov 17 '18 at 15:15
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%2f53342896%2fhow-to-implement-every-and-no-operations-for-a-joined-list-table%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