Match ALL (not ANY) values from a WHERE clause [duplicate]



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2
















This question already has an answer here:



  • SQL efficient way to join a table where all values exist

    3 answers



TL;DR How do I find rows in a table that match ALL (not ANY) rows from another table?



This seems so simple but I don't know the correct terminology, so am seeing dozens of answers that use INNER JOIN, INTERSECT, EXISTS or ALL, but don't achieve what I need. The other questions are either PostgreSQL, dynamically generated SQL via the application, or are unanswered.



Take the following people who like different colors:



DECLARE @tbl TABLE (
FirstName nvarchar(50),
Color nvarchar(50)
);
INSERT INTO @tbl
(FirstName, Color)
VALUES
('Bob', 'Purple'),
('Bob', 'Red'),
('Bob', 'Yellow'),
('Fred', 'Purple'),
('Fred', 'Red'),
('Fred', 'Yellow'),
('Greg', 'Orange'),
('Greg', 'Red'),
('Harry', 'Red');


I need to find people who like ALL of the colors I'm searching for.



DECLARE @SearchColors TABLE (SearchColor nvarchar(50));
INSERT INTO @SearchColors (SearchColor) VALUES ('Red'),('Yellow');


So I would only expect to see Bob and Fred in the results, because only those two people like ALL of the colours I'm searching for. I don't want people who only like a single colour, however, it doesn't matter if people like more than both of those colours (e.g. Bob likes 3 colors, including the two I need).



Reading through books online, I found ALL, which appeared close to what I need, but actually finds nothing (unless I'm using it wrong):



SELECT
*
FROM
@tbl
WHERE
(Color = ALL ( SELECT SearchColor FROM @SearchColors ));









share|improve this question















marked as duplicate by Zohar Peled, Community Nov 14 '18 at 8:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.

    – EvilDr
    Nov 14 '18 at 8:27











  • With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)

    – Zohar Peled
    Nov 14 '18 at 8:28

















2
















This question already has an answer here:



  • SQL efficient way to join a table where all values exist

    3 answers



TL;DR How do I find rows in a table that match ALL (not ANY) rows from another table?



This seems so simple but I don't know the correct terminology, so am seeing dozens of answers that use INNER JOIN, INTERSECT, EXISTS or ALL, but don't achieve what I need. The other questions are either PostgreSQL, dynamically generated SQL via the application, or are unanswered.



Take the following people who like different colors:



DECLARE @tbl TABLE (
FirstName nvarchar(50),
Color nvarchar(50)
);
INSERT INTO @tbl
(FirstName, Color)
VALUES
('Bob', 'Purple'),
('Bob', 'Red'),
('Bob', 'Yellow'),
('Fred', 'Purple'),
('Fred', 'Red'),
('Fred', 'Yellow'),
('Greg', 'Orange'),
('Greg', 'Red'),
('Harry', 'Red');


I need to find people who like ALL of the colors I'm searching for.



DECLARE @SearchColors TABLE (SearchColor nvarchar(50));
INSERT INTO @SearchColors (SearchColor) VALUES ('Red'),('Yellow');


So I would only expect to see Bob and Fred in the results, because only those two people like ALL of the colours I'm searching for. I don't want people who only like a single colour, however, it doesn't matter if people like more than both of those colours (e.g. Bob likes 3 colors, including the two I need).



Reading through books online, I found ALL, which appeared close to what I need, but actually finds nothing (unless I'm using it wrong):



SELECT
*
FROM
@tbl
WHERE
(Color = ALL ( SELECT SearchColor FROM @SearchColors ));









share|improve this question















marked as duplicate by Zohar Peled, Community Nov 14 '18 at 8:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.

    – EvilDr
    Nov 14 '18 at 8:27











  • With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)

    – Zohar Peled
    Nov 14 '18 at 8:28













2












2








2









This question already has an answer here:



  • SQL efficient way to join a table where all values exist

    3 answers



TL;DR How do I find rows in a table that match ALL (not ANY) rows from another table?



This seems so simple but I don't know the correct terminology, so am seeing dozens of answers that use INNER JOIN, INTERSECT, EXISTS or ALL, but don't achieve what I need. The other questions are either PostgreSQL, dynamically generated SQL via the application, or are unanswered.



Take the following people who like different colors:



DECLARE @tbl TABLE (
FirstName nvarchar(50),
Color nvarchar(50)
);
INSERT INTO @tbl
(FirstName, Color)
VALUES
('Bob', 'Purple'),
('Bob', 'Red'),
('Bob', 'Yellow'),
('Fred', 'Purple'),
('Fred', 'Red'),
('Fred', 'Yellow'),
('Greg', 'Orange'),
('Greg', 'Red'),
('Harry', 'Red');


I need to find people who like ALL of the colors I'm searching for.



DECLARE @SearchColors TABLE (SearchColor nvarchar(50));
INSERT INTO @SearchColors (SearchColor) VALUES ('Red'),('Yellow');


So I would only expect to see Bob and Fred in the results, because only those two people like ALL of the colours I'm searching for. I don't want people who only like a single colour, however, it doesn't matter if people like more than both of those colours (e.g. Bob likes 3 colors, including the two I need).



Reading through books online, I found ALL, which appeared close to what I need, but actually finds nothing (unless I'm using it wrong):



SELECT
*
FROM
@tbl
WHERE
(Color = ALL ( SELECT SearchColor FROM @SearchColors ));









share|improve this question

















This question already has an answer here:



  • SQL efficient way to join a table where all values exist

    3 answers



TL;DR How do I find rows in a table that match ALL (not ANY) rows from another table?



This seems so simple but I don't know the correct terminology, so am seeing dozens of answers that use INNER JOIN, INTERSECT, EXISTS or ALL, but don't achieve what I need. The other questions are either PostgreSQL, dynamically generated SQL via the application, or are unanswered.



Take the following people who like different colors:



DECLARE @tbl TABLE (
FirstName nvarchar(50),
Color nvarchar(50)
);
INSERT INTO @tbl
(FirstName, Color)
VALUES
('Bob', 'Purple'),
('Bob', 'Red'),
('Bob', 'Yellow'),
('Fred', 'Purple'),
('Fred', 'Red'),
('Fred', 'Yellow'),
('Greg', 'Orange'),
('Greg', 'Red'),
('Harry', 'Red');


I need to find people who like ALL of the colors I'm searching for.



DECLARE @SearchColors TABLE (SearchColor nvarchar(50));
INSERT INTO @SearchColors (SearchColor) VALUES ('Red'),('Yellow');


So I would only expect to see Bob and Fred in the results, because only those two people like ALL of the colours I'm searching for. I don't want people who only like a single colour, however, it doesn't matter if people like more than both of those colours (e.g. Bob likes 3 colors, including the two I need).



Reading through books online, I found ALL, which appeared close to what I need, but actually finds nothing (unless I'm using it wrong):



SELECT
*
FROM
@tbl
WHERE
(Color = ALL ( SELECT SearchColor FROM @SearchColors ));




This question already has an answer here:



  • SQL efficient way to join a table where all values exist

    3 answers







tsql sql-server-2012






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 22:27









vencaslac

1,183420




1,183420










asked Nov 13 '18 at 22:25









EvilDrEvilDr

3,93674490




3,93674490




marked as duplicate by Zohar Peled, Community Nov 14 '18 at 8:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Zohar Peled, Community Nov 14 '18 at 8:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.

    – EvilDr
    Nov 14 '18 at 8:27











  • With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)

    – Zohar Peled
    Nov 14 '18 at 8:28

















  • Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.

    – EvilDr
    Nov 14 '18 at 8:27











  • With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)

    – Zohar Peled
    Nov 14 '18 at 8:28
















Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.

– EvilDr
Nov 14 '18 at 8:27





Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.

– EvilDr
Nov 14 '18 at 8:27













With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)

– Zohar Peled
Nov 14 '18 at 8:28





With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)

– Zohar Peled
Nov 14 '18 at 8:28












1 Answer
1






active

oldest

votes


















1














What about getting the total count of colors you are looking for, filtering your main table for those, then counting names that have that many entries?



DECLARE @colorcount INT = (SELECT COUNT(DISTINCT SearchColor) FROM @SearchColors)

SELECT firstname
FROM @tbl
WHERE color IN (SELECT searchcolor FROM @SearchColors)
GROUP BY firstname
HAVING COUNT(DISTINCT color) = @colorcount


This will handle dupes in your @tbl if ('Greg', 'Red') is in there twice or if your @searchcolors table has ('Red') twice






share|improve this answer

























  • Thank you. I was massively overthinking this with an overtired mind...

    – EvilDr
    Nov 14 '18 at 8:28

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














What about getting the total count of colors you are looking for, filtering your main table for those, then counting names that have that many entries?



DECLARE @colorcount INT = (SELECT COUNT(DISTINCT SearchColor) FROM @SearchColors)

SELECT firstname
FROM @tbl
WHERE color IN (SELECT searchcolor FROM @SearchColors)
GROUP BY firstname
HAVING COUNT(DISTINCT color) = @colorcount


This will handle dupes in your @tbl if ('Greg', 'Red') is in there twice or if your @searchcolors table has ('Red') twice






share|improve this answer

























  • Thank you. I was massively overthinking this with an overtired mind...

    – EvilDr
    Nov 14 '18 at 8:28















1














What about getting the total count of colors you are looking for, filtering your main table for those, then counting names that have that many entries?



DECLARE @colorcount INT = (SELECT COUNT(DISTINCT SearchColor) FROM @SearchColors)

SELECT firstname
FROM @tbl
WHERE color IN (SELECT searchcolor FROM @SearchColors)
GROUP BY firstname
HAVING COUNT(DISTINCT color) = @colorcount


This will handle dupes in your @tbl if ('Greg', 'Red') is in there twice or if your @searchcolors table has ('Red') twice






share|improve this answer

























  • Thank you. I was massively overthinking this with an overtired mind...

    – EvilDr
    Nov 14 '18 at 8:28













1












1








1







What about getting the total count of colors you are looking for, filtering your main table for those, then counting names that have that many entries?



DECLARE @colorcount INT = (SELECT COUNT(DISTINCT SearchColor) FROM @SearchColors)

SELECT firstname
FROM @tbl
WHERE color IN (SELECT searchcolor FROM @SearchColors)
GROUP BY firstname
HAVING COUNT(DISTINCT color) = @colorcount


This will handle dupes in your @tbl if ('Greg', 'Red') is in there twice or if your @searchcolors table has ('Red') twice






share|improve this answer















What about getting the total count of colors you are looking for, filtering your main table for those, then counting names that have that many entries?



DECLARE @colorcount INT = (SELECT COUNT(DISTINCT SearchColor) FROM @SearchColors)

SELECT firstname
FROM @tbl
WHERE color IN (SELECT searchcolor FROM @SearchColors)
GROUP BY firstname
HAVING COUNT(DISTINCT color) = @colorcount


This will handle dupes in your @tbl if ('Greg', 'Red') is in there twice or if your @searchcolors table has ('Red') twice







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 22:36

























answered Nov 13 '18 at 22:30









dfundakodfundako

5,0813924




5,0813924












  • Thank you. I was massively overthinking this with an overtired mind...

    – EvilDr
    Nov 14 '18 at 8:28

















  • Thank you. I was massively overthinking this with an overtired mind...

    – EvilDr
    Nov 14 '18 at 8:28
















Thank you. I was massively overthinking this with an overtired mind...

– EvilDr
Nov 14 '18 at 8:28





Thank you. I was massively overthinking this with an overtired mind...

– EvilDr
Nov 14 '18 at 8:28





Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

PHP code is not being executed, instead code shows on the page

Ĝň ċũ,İĨĻĐđēĵſ ĤŹĝĽķũ ĆŹđě,ļ łĂģĚ ŻŷĶĸĦķĖĝŔ ūŬĿŜćīŴĽĺ,ľ,ũĽňĎźāœ,ĉŃĩ,ĎĻē,ŕųūČœijčĶŶĻijŕ,ĩěŗſŗāŸņŰž,Žů ĭğŪľ Čŗćļžĵţ Ūĉņ ĵ ůŁ ţŅ,ĮŶ ŁŵĮ śŁĬŇŏ ŮĜʼnœĦā,Ũŏ Ź ŖŀšĦ ŃłĉŹŅťŸŇ,ŷī