473,404 Members | 2,137 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,404 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 1094

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...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.