[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Finding orphans and "childless parents" in mysql tables

Posted on 2004-11-08
3
Medium Priority
?
471 Views
Last Modified: 2006-11-17
I'm using mysql 4.0.10 (no subqueries until 4.1).

I have a master table containing a few million rows and a child table about 3x larger.
The association is many-to-1, so each row in the master table is associated with 1 to N rows in child.
All the IDs are non-null.

table MOM:
parent_ID, example {1,2,3,4,6}
more stuff...

table KID:
child_ID,
parent_ID, example {1,3,3,1,3,4,5,6,7}
more stuff...

I need a query to find the orphans in KID, and another one to find the ones in MOM that have no "children" in KID.
Something to find the orphans:
-> returns the ones with parent_ID=5 and 7 because it's not in MOM

Something to find the rows im MOM noone is connected to
-> returns the one with parent_ID=2, because it's not in KID

I'm using mysql 4.0.10 so subqueries like:
select *  from KID where parent_ID not in  (select parent_ID from MOM);
are not allowed - how can I do this with a JOIN or something else?
0
Comment
Question by:bytta
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
virmaior earned 2000 total points
ID: 12526271
SELECT * FROM KID LEFT JOIN MOM ON KID.parent_ID = MOM.parent_ID WHERE MOM.parent_ID IS NULL

that will show you the orphaned children

SELECT * FROM MOM LEFT JOIN KID ON MOM.parent_ID = KID.parent_ID WHERE KID.parent_ID IS NULL

and  the bereaved mothers
0
 
LVL 4

Author Comment

by:bytta
ID: 12527518
Silly me...

I saw this here:
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_20333988.html
and here:
http://www.experts-exchange.com/Databases/Mysql/Q_21142426.html

Then I wrote ".... WHERE KID.parent_ID = NULL"

Using "=" instead of "IS" gives an empty set, so I was sure this wouldn't work...

Easy points to virmaior...
0
 
LVL 20

Expert Comment

by:virmaior
ID: 12527689
I made the same mistake a while ago, now I'll never forget (IS NULL) !=  (= NULL)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question