473,387 Members | 1,611 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,387 software developers and data experts.

complicated problem?

I am doing a contract job for a client with
some existing messy data.

I am trying to find a solution to a problem of
many-to-many mappings between two tables in
a database. See below for my contrived example.
Tables a and b each have simple primary keys.
They also have 2 columns that are related to
each other. In other words, a1 and b1 carry the
same "key" type of data. Same thing with a2 and
b2. I need to locate all of the "groups" of records
in both table a and table b where there is
more than one record in both tables. Man, this is
even difficult to state...

DROP DATABASE IF EXISTS many;
CREATE DATABASE many;
USE many;

CREATE TABLE a
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

a1 INT NOT NULL,
INDEX(a1),

a2 INT NOT NULL,
INDEX(a2)
);

CREATE TABLE b
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

b1 INT NOT NULL,
INDEX(a1),

b2 INT NOT NULL,
INDEX(a2)
);

INSERT INTO a (a1,a2) VALUES (1,8);
INSERT INTO a (a1,a2) VALUES (1,2);
INSERT INTO a (a1,a2) VALUES (2,1);
INSERT INTO a (a1,a2) VALUES (1,8);
INSERT INTO a (a1,a2) VALUES (1,1);
INSERT INTO a (a1,a2) VALUES (1,1);
INSERT INTO a (a1,a2) VALUES (4,4);
INSERT INTO a (a1,a2) VALUES (1,2);
INSERT INTO a (a1,a2) VALUES (1,2);

INSERT INTO b (b1,b2) VALUES (1,2);
INSERT INTO b (b1,b2) VALUES (2,1);
INSERT INTO b (b1,b2) VALUES (1,8);
INSERT INTO b (b1,b2) VALUES (1,8);
INSERT INTO b (b1,b2) VALUES (3,3);
INSERT INTO b (b1,b2) VALUES (1,2);

OK, so in my contrived example, I would like to
retrieve groups as follows:

group: table a: #1, #4; table b: #3, #4;

group: table a: #2, #8, #9; table b: #1, #6;

Hopefully, you follow my notation.
Is there a way to do this in straight SQL? If not,
how would you do it? The tables are huge, with 10's
of millions of records in each table.

Thanks in advance.
Dean Hoover

Jul 19 '05 #1
0 1318

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

Similar topics

7
by: Bo Peng | last post by:
Dear Python group: I am planning on an application that involves several complicated C++ classes. Basically, there will be one or two big data objects and some "action" objects that can act on...
15
by: Agoston Bejo | last post by:
Hi, I'm having trouble with implementing some constraints on the database level. An example: --Table A(AID, BID ref. B.BID, ATXT) --Table B(BID, CID ref. C.CID) --Table C(CID) upon insertion...
4
by: Ulrich Albrecht | last post by:
Hi, I'm making a complicated css based site (CMS) without tables (because of Accessibility). It looks good in any new browsers on my machine (IE5.5, IE6, NS7, OP6, Mozilla 1.1) :-) You can...
4
by: Tom Schmitt | last post by:
I tried getting a complicated, multi-column box design on it's way on my web-site and it works out fine on the IE6. Because I'm a relative beginner to all stuff related to CSS and am used to...
1
by: Johm | last post by:
I have a very complicated problem.I have a function that checks whether the cartons in the warehouse,which are branch0, are greater than the cartons ordered. Which means that If branch < cartons...
26
by: Jeff | last post by:
Ok gang. Here is something complicated, well, at least to me anyway. Using Access DB I have a table in my DB called members. In that table, I have 2 tables I will be using "username" and...
3
by: Henrry Pires | last post by:
Hello to all. I have and strange and complicated problem. My asp.net application raise errors when the data that the user input is invalid. For example: The user didn't fill all some data...
3
by: Charlie Bear | last post by:
i've got myself into a bit of an oo mess. it's probably me misunderstanding how oo works. I've got a base class called "Feature" which some classes inherit. in the database i've stored the data...
11
by: CoreyWhite | last post by:
So I'm reading books about perl, which may not be quite as powerful as C++ but at least has more power than C & is very easy to learn from the manuals. I'm also buying books on C++, and books...
4
by: Horacius ReX | last post by:
Hi, I have to read some data from a file, and on each block it always appears the followng string; xyz.vs.1-81_1 . It appears a lot of time with different numbers like; xyz.vs.1-81_1...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.