Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got 2 queries, A:
SELECT sites.id, sites.name, 
FROM sites 
INNER JOIN domains
ON sites.id = domains.site
WHERE domains.is_deleted = 1

And B
SELECT sites.id, sites.name, 
FROM sites 
INNER JOIN domains
ON sites.id = domains.site
WHERE domains.is_deleted = 0


The first selects all sites that have domains that are deleted. The second selects all sites that have domains that are not deleted. A single site can have multiple domains.

Is there a join where I can remove all of the intersections, so that I am left with a collection of sites that only have all deleted (or not-deleted) domains?

And if there is, where can I find the syntax to query the results of queries?

What I have tried:

I've been trying to read through documents, and asking on other forums. I've mostly been getting... relatively unkind words, and I don't really understand why.

I don't know what I don't know, and I would like to know where to look so that I can learn it.
Posted
Updated 7-Jun-21 22:17pm
v2

Quote:
I am left with a collection of sites that only have all deleted (or not-deleted) domains?
Your current queries will return duplicate sites, one for each domain in the site.

Change your current queries to:
SQL
/* At least one domain deleted: */
SELECT id, name
FROM sites
WHERE EXISTS
(
    SELECT 1
    FROM domains
    WHERE domains.site = sites.id
    AND domains.is_deleted = 1
);

/* At least one domain not deleted: */
SELECT id, name
FROM sites
WHERE EXISTS
(
    SELECT 1
    FROM domains
    WHERE domains.site = sites.id
    AND domains.is_deleted = 0
);

It should then be easier to see how to achieve your other queries:
SQL
/* All domains deleted: */
SELECT id, name
FROM sites
WHERE NOT EXISTS
(
    SELECT 1
    FROM domains
    WHERE domains.site = sites.id
    AND domains.is_deleted = 0
);

/* No domains deleted: */
SELECT id, name
FROM sites
WHERE NOT EXISTS
(
    SELECT 1
    FROM domains
    WHERE domains.site = sites.id
    AND domains.is_deleted = 1
);
 
Share this answer
 
Comments
Sango Dragon 8-Jun-21 11:35am    
I'll try it when I get back home. This shows promise. Do you mind explaining the select 1? Is that equivalent to select *?
Richard Deeming 8-Jun-21 11:47am    
An EXISTS / NOT EXISTS query doesn't actually select any columns.

You could specify one or more column names in the query, but it wouldn't make any difference. In some cases, you can even specify expressions which should generate an error - eg: SELECT 1/0.

The usual practise is to use either SELECT * or SELECT 1, depending on personal preference.

There are various blogs discussing this - for example:
Actual Difference Between EXISTS(SELECT 1 …), EXISTS(SELECT * …), and EXISTS(SELECT column …) – Sql Quantum Leap[^]
There is no intersection: domains.is_deleted cannot be both zero and one at the same time!

I think you need to think carefully about exactly what you are trying to do: what you describe cannot happen!
 
Share this answer
 
Comments
Sango Dragon 8-Jun-21 1:15am    
My bad, I didn't make it explicit enough. I edited it in saying that a site can have multiple domains. And so a site can have a domain with 0, and a domain with 1.
OriginalGriff 8-Jun-21 1:28am    
Do you want to try showing us sample data so it's clear what is where and what you expect to get? It might save us all going round the loop a coi=uple of dozen times! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900