PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Logiciels d'hébergement > mailing.database.mysql > Moderately complex query (how-to?)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Moderately complex query (how-to?)

Réponse
 
LinkBack Outils de la discussion
Vieux 10/04/2006, 15h32   #1
Sam
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Moderately complex query (how-to?)

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

  Réponse avec citation
Vieux 10/04/2006, 17h27   #2
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Moderately complex query (how-to?)

>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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h45.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,08878 seconds with 10 queries