/    Sign up×
Community /Pin to ProfileBookmark

Where an ID doesn’t exist

I have two tables, a main table with data and indexed by and ID.

I have a second table, which has this id and another linking the two.

I want to find rows in my main table that aren’t in my linking second table. Essentially orphan rows.

I thought maybe like this but doesn’t work. Any ideas?

“`SELECT m.ID, COUNT(l.ID) FROM `mainTable` m JOIN `linkingTable` l ON m.ID = l.ID“`

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 23.2021 — I might do...
[code=sql]
select ID from mainTable where ID not in(
select ID from linkingTable
)
[/code]
Copy linkTweet thisAlerts:
@kiwisauthorMar 23.2021 — Ended up with this?

SELECT m.ID FROM mainTable m<br/>
LEFT JOIN
linkingTable l ON l.Dd = m.ID<br/>
WHERE l.ID IS NULL
Copy linkTweet thisAlerts:
@NogDogMar 23.2021 — Should work, too. Not sure if either would be more efficient -- or if the database engine would essentially optimize both to do the same thing.
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...