Home > Back-end >  SQL Section: Only Names, that are exists on one entry
SQL Section: Only Names, that are exists on one entry

Time:12-06

I have an DB with two columns names and day. I want to find all names that exists only once at day x. Really simple, but my brain doesn't work .

I thought a simple self join would do this but..

SELECT d1.id 
FROM day AS d1 
JOIN day AS d2 ON d1.id = d2.id 
AND d1.day=3 AND d1.day != d2.day
Example:
| name     | day          |
| -------- | ------------ |
| First    | 1            |
| First    | 2            |
| First    | 3            |
| Second   | 3            |

and want only return Second

CodePudding user response:

Does the following get your expected results? Select the desired rows and check rows for other days do not exist

Select d.name
from Day d
where d.day=3
and not exists (
    select * from Day d2
    where d2.name = d.name and d2.day != d.day
)
  • Related