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

Moderately complex query (how-to?)

Sam
For this example I have eliminated (most of) the unreferenced
columns from the table definitions.

My problem is: how does one formulate a select statement or
create a view in SQL (I'm using MySQL (Ver 14.7 Distrib 4.1.16))
that implements the pseudo-code that follows the table
definitions? BTW, I _have_ read the manual (several times).

create table t2 ( # about 1K rows
n2 smallint unsigned not null auto_increment unique key,
n1 smallint unsigned not null references t1 (n1),
s2 varchar(90) primary key);

create table t3 ( # about 50K rows
n3 mediumint unsigned not null auto_increment unique key,
s3 varchar(99) primary key);

create table t4 ( # about 1.6M rows
k4 char(15) primary key,
dn mediumint unsigned references t3 (n3),
vn smallint unsigned references t2 (n2),
s4 varchar(120) not null);

create table t5 ( # about 30K rows
k5 char(15) not null key references t4 (ik),
vn smallint unsigned not null references t2 (n2));

If I were doing this in a procedural language, I would:

loop for each row of t5
using row from t4 with t4.k4==t5.k5
// row always exists and t4.vn != t5.vn when t4.k4==t5.k5
if dn NULL go to top of loop end_if // 60%-80% are NULL
display t1.s1 as ac where t1.n1==t5.vn
display t1.s1 as ai where t1.n1==t4.vn
display t4.s4 as fn
display t3.s3 as di where t3.n3==t4.dn
end_using
end_loop

TIA

Apr 10 '06 #1
1 1130
>For this example I have eliminated (most of) the unreferenced
columns from the table definitions.

My problem is: how does one formulate a select statement or
create a view in SQL (I'm using MySQL (Ver 14.7 Distrib 4.1.16))
I don't think you get views in MySQL until 5.0.
that implements the pseudo-code that follows the table
definitions? BTW, I _have_ read the manual (several times).

create table t2 ( # about 1K rows
n2 smallint unsigned not null auto_increment unique key,
n1 smallint unsigned not null references t1 (n1),
s2 varchar(90) primary key);

create table t3 ( # about 50K rows
n3 mediumint unsigned not null auto_increment unique key,
s3 varchar(99) primary key);

create table t4 ( # about 1.6M rows
k4 char(15) primary key,
dn mediumint unsigned references t3 (n3),
vn smallint unsigned references t2 (n2),
s4 varchar(120) not null);

create table t5 ( # about 30K rows
k5 char(15) not null key references t4 (ik),
vn smallint unsigned not null references t2 (n2));

If I were doing this in a procedural language, I would:

loop for each row of t5
using row from t4 with t4.k4==t5.k5
// row always exists and t4.vn != t5.vn when t4.k4==t5.k5
if dn NULL go to top of loop end_if // 60%-80% are NULL
display t1.s1 as ac where t1.n1==t5.vn
display t1.s1 as ai where t1.n1==t4.vn
display t4.s4 as fn
display t3.s3 as di where t3.n3==t4.dn
end_using
end_loop


SELECT
t1a.s1 as ac,
t1b.s1 as ai,
t4.s4 as fn,
t3.s3 as di
FROM t5
LEFT JOIN t4 ON t4.k4 = t5.k5
LEFT JOIN t1 as t1a ON t1a.n1 = t5.vn
LEFT JOIN t1 as t1b ON t1b.n1 = t4.vn
LEFT JOIN t3 ON t3.n3 = t4.dn
WHERE t4.dn is not null and t4.vn != t5.vn;

I didn't check all the foreign key references to see if there would
be a difference between a left join and a regular join (whether
matching records are guaranteed to exist or not), and in any case,
you didn't provide a table schema for table t1. I suspect you
really meant for references to t1 to be to t2.

You can join against the same table twice by using aliases to give
each copy different names, then using the aliases to reference
fields.

Gordon L. Burditt
Apr 10 '06 #2

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

Similar topics

0
by: awarsd | last post by:
------=_NextPart_000_0007_01C34C8B.2CF5D7A0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, I'm looking at some search engine where we can have...
0
by: schan | last post by:
Hi there, I was wondering if someone could shed some light on a problem I have no idea on how to fix. I created an Excel Add-In that uses an ADO connection to an Access database on a file...
3
blyxx86
by: blyxx86 | last post by:
Hey there, I'm running into a slight problem today... I have a few things to show... I'm running into duplicate values being shown in my query, but I cannot use a "Select DISTINCT" as it...
5
by: binky | last post by:
Question for all you SQL gurus out there. I have a (seemingly) complex query to write, and since I'm just learning SQL server, am not sure how to go about it. Tables: t_trans t_account All...
1
by: Rudolf Bargholz | last post by:
Hi, We have created triggers to log modifications to tables in our application. The triggers work fine, just on one of the tables in our database the triggers fail with the error message...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
2
BeemerBiker
by: BeemerBiker | last post by:
I put together a (what I consider) complex query using the Access wizard. It works fine in access but fails when I code it up. I can actually make it work in code by "simplifying" it, but then it...
3
by: william67 | last post by:
I'm having a hard time building a query to do what I need to do and was hoping some genius could help me out, I need to do a complex query and any and all help is much appreciated this is the...
0
by: Kozy | last post by:
Hello everyone, i have a big problem with imports. I have only see this problem on 9.5, i have never goten it on 9.1. 99% of time db2 works great ( version 9.5 fixpack 5 ) on windows 2008 (...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...

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.