473,394 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

mysql getting orphan records


I'm working with mysql without referential itegrity.
Let me make some small example:

tableA
+-----------+---------------+
| id_1 | data_1 |
+-----------+---------------+
| 1 | bla1 |
| 2 | bla2 |
| 3 | bla3 |
+-----------+---------------+

tableB

+-----------+------------+
| id_2 | fk_1 |
+-----------+------------+
| 2_1 | 1 |
| 2_2 | 3 |
| 2_3 | 6 |
+-----------+------------+

Every record on tableA, should have at least 1 record on tableB, linked
trhough fk_1, but as times goes

on, and due users doing wrong things, there could be problems.

For instance, in the above example, there is one record in tableA
(id_1=2) that has no related record on

tableB , and there is one record in tableB (id_2=2_3) whose fk_1 value
doesn't exists on tableA

Now the question:
1) How could I select all records on tableA which has no related
records on tableB?
2) How could I select all records on tableB which has no related
records on tableA?

I've been tring with left and right joins, but I can't figure out how
to do in order to get just "orphan"

records instead full sets

regards - julian

Jun 30 '06 #1
2 3982
julian_m wrote:
Now the question:
1) How could I select all records on tableA which has no related
records on tableB?
SELECT tableA.id_1
FROM tableA LEFT OUTER JOIN tableB ON tableA.id_1 = tableB.fk_1
WHERE tableB.fk_1 IS NULL;
2) How could I select all records on tableB which has no related
records on tableA?


SELECT tableB.id_2
FROM tableA RIGHT OUTER JOIN tableB ON tableA.id_1 = tableB.fk_1
WHERE tableA.id_1 IS NULL;

Regards,
Bill K.
Jun 30 '06 #2
Many thanks Bill.

regards- julian

Jun 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: jackiu | last post by:
------=_NextPart_000_0001_01C35286.10843160 Content-Type: multipart/alternative; boundary="----=_NextPart_001_0002_01C35286.10843160" ------=_NextPart_001_0002_01C35286.10843160 Content-Type:...
0
by: John Thorne | last post by:
Have 2 tables ts_sheets : TimeSheet_ID TimeSheet_Date Emp_ID Orphan LineItem TimeSheet_ID Client
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
3
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE:...
2
by: David | last post by:
Hi, Has anyone had this problem ? I am using MySQL ODBC 3.51 Driver, with MS Access 2003 and MySQL 4.1.11 standard log. I created my tables in MS Access, then exported them via ODBC to an...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
15
by: l3vi | last post by:
I have a new system Im building that stores entries of what people are searching for on my sites. I want to be able to keep records of how many times a keyword was searched for daily, and from...
5
by: RioRanchoMan | last post by:
I have a forum table where the field Forum_ID of the first thread corresponds to itself in the field Forum_Ancestor, and 0 (zero) for the field Forum_Parent when it is the first topic in a thread:...
8
by: menmysql | last post by:
i am not bale to solve this problem since two weeks i am trying to access records from mysql database using jsp. inside this jsp program i wrote all my JDBC code. it is working very nicely and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.