30. august 2007 - 16:58
#3
Okay, du vil hellere have det som et opslag i en tabel og foretaget i SQL. Ja, det er sikkert også det mest fornuftige og gennemtænkte set i en vedligeholdelsessammenhæng.
Okay, så vil jeg foreslå, at du opretter en ekstra tabel med kombinationer og tilhørende point (eller hvad det nu er) for de ord som bare skal forekomme.
Nu antager jeg, at du har disse tre tabeller:
dinOrdTabel
Id, Ord1, Ord2, Ord3
dinKombiordPointTabel
Id, Ord1, Ord2, Ord3, Point
dinForekomstOrdPointTabel
Id, Ord, Point
hvor dinOrdTabel er det udgangspunkt du har og de to andre indeholder ordene og deres tilhørende point.
Så burde du kunne gøre sådan her:
SELECT o.Ord1, o.Ord2, o.Ord3, o.Ord1 & o.Ord2 & o.Ord3 AS Kombiord, Nz(p1.Point, Nz(p2.Point, 0)) AS Point
FROM dinOrdTabel o
LEFT JOIN dinForekomstordPointTabel p1 ON (o.Ord1 = p1.Ord Or o.Ord2 = p1.Ord Or o.Ord3 = p1.Ord)
LEFT JOIN dinKombiordPointTabel p2 ON
(Nz(o.Ord1,"")=Nz(p2.Ord1,"") And Nz(o.Ord2,"")=Nz(p2.Ord2,"") And Nz(o.Ord3,"")=Nz(p2.Ord3,"")) Or (Nz(o.Ord1,"")=Nz(p2.Ord1,"") And Nz(o.Ord3,"")=Nz(p2.Ord2,"") And Nz(o.Ord2,"")=Nz(p2.Ord3,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord1,"") And Nz(o.Ord1,"")=Nz(p2.Ord2,"") And Nz(o.Ord3,"")=Nz(p2.Ord3,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord1,"") And Nz(o.Ord3,"")=Nz(p2.Ord2,"") And Nz(o.Ord1,"")=Nz(p2.ord3,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord1,"") And Nz(o.Ord1,"")=Nz(p2.Ord2,"") And Nz(o.Ord2,"")=Nz(p2.Ord3,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord1,"") And Nz(o.Ord2,"")=Nz(p2.Ord2,"") And Nz(o.Ord1,"")=Nz(p2.Ord3,""))
Or
(Nz(o.Ord1,"")=Nz(p2.Ord1,"") And Nz(o.Ord3,"")=Nz(p2.Ord3,"") And Nz(o.Ord3,"")=Nz(p2.Ord2,"")) Or (Nz(o.Ord1,"")=Nz(p2.Ord1,"") And Nz(o.Ord3,"")=Nz(p2.Ord3,"") And Nz(o.Ord2,"")=Nz(p2.Ord2,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord1,"") And Nz(o.Ord1,"")=Nz(p2.Ord3,"") And Nz(o.Ord3,"")=Nz(p2.Ord2,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord1,"") And Nz(o.Ord3,"")=Nz(p2.Ord3,"") And Nz(o.Ord1,"")=Nz(p2.ord2,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord1,"") And Nz(o.Ord1,"")=Nz(p2.Ord3,"") And Nz(o.Ord2,"")=Nz(p2.Ord2,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord1,"") And Nz(o.Ord2,"")=Nz(p2.Ord3,"") And Nz(o.Ord1,"")=Nz(p2.Ord2,""))
Or
(Nz(o.Ord1,"")=Nz(p2.Ord2,"") And Nz(o.Ord3,"")=Nz(p2.Ord1,"") And Nz(o.Ord3,"")=Nz(p2.Ord3,"")) Or (Nz(o.Ord1,"")=Nz(p2.Ord2,"") And Nz(o.Ord3,"")=Nz(p2.Ord1,"") And Nz(o.Ord2,"")=Nz(p2.Ord3,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord2,"") And Nz(o.Ord1,"")=Nz(p2.Ord1,"") And Nz(o.Ord3,"")=Nz(p2.Ord3,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord2,"") And Nz(o.Ord3,"")=Nz(p2.Ord1,"") And Nz(o.Ord1,"")=Nz(p2.ord3,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord2,"") And Nz(o.Ord1,"")=Nz(p2.Ord1,"") And Nz(o.Ord2,"")=Nz(p2.Ord3,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord2,"") And Nz(o.Ord2,"")=Nz(p2.Ord1,"") And Nz(o.Ord1,"")=Nz(p2.Ord3,""))
Or
(Nz(o.Ord1,"")=Nz(p2.Ord2,"") And Nz(o.Ord3,"")=Nz(p2.Ord3,"") And Nz(o.Ord3,"")=Nz(p2.Ord1,"")) Or (Nz(o.Ord1,"")=Nz(p2.Ord2,"") And Nz(o.Ord3,"")=Nz(p2.Ord3,"") And Nz(o.Ord2,"")=Nz(p2.Ord1,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord2,"") And Nz(o.Ord1,"")=Nz(p2.Ord3,"") And Nz(o.Ord3,"")=Nz(p2.Ord1,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord2,"") And Nz(o.Ord3,"")=Nz(p2.Ord3,"") And Nz(o.Ord1,"")=Nz(p2.ord1,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord2,"") And Nz(o.Ord1,"")=Nz(p2.Ord3,"") And Nz(o.Ord2,"")=Nz(p2.Ord1,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord2,"") And Nz(o.Ord2,"")=Nz(p2.Ord3,"") And Nz(o.Ord1,"")=Nz(p2.Ord1,""))
Or
(Nz(o.Ord1,"")=Nz(p2.Ord3,"") And Nz(o.Ord3,"")=Nz(p2.Ord1,"") And Nz(o.Ord3,"")=Nz(p2.Ord2,"")) Or (Nz(o.Ord1,"")=Nz(p2.Ord3,"") And Nz(o.Ord3,"")=Nz(p2.Ord1,"") And Nz(o.Ord2,"")=Nz(p2.Ord2,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord3,"") And Nz(o.Ord1,"")=Nz(p2.Ord1,"") And Nz(o.Ord3,"")=Nz(p2.Ord2,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord3,"") And Nz(o.Ord3,"")=Nz(p2.Ord1,"") And Nz(o.Ord1,"")=Nz(p2.ord2,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord3,"") And Nz(o.Ord1,"")=Nz(p2.Ord1,"") And Nz(o.Ord2,"")=Nz(p2.Ord2,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord3,"") And Nz(o.Ord2,"")=Nz(p2.Ord1,"") And Nz(o.Ord1,"")=Nz(p2.Ord2,""))
Or
(Nz(o.Ord1,"")=Nz(p2.Ord3,"") And Nz(o.Ord3,"")=Nz(p2.Ord2,"") And Nz(o.Ord3,"")=Nz(p2.Ord1,"")) Or (Nz(o.Ord1,"")=Nz(p2.Ord3,"") And Nz(o.Ord3,"")=Nz(p2.Ord2,"") And Nz(o.Ord2,"")=Nz(p2.Ord1,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord3,"") And Nz(o.Ord1,"")=Nz(p2.Ord2,"") And Nz(o.Ord3,"")=Nz(p2.Ord1,"")) Or (Nz(o.Ord2,"")=Nz(p2.Ord3,"") And Nz(o.Ord3,"")=Nz(p2.Ord2,"") And Nz(o.Ord1,"")=Nz(p2.ord1,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord3,"") And Nz(o.Ord1,"")=Nz(p2.Ord2,"") And Nz(o.Ord2,"")=Nz(p2.Ord1,"")) Or (Nz(o.Ord3,"")=Nz(p2.Ord3,"") And Nz(o.Ord2,"")=Nz(p2.Ord2,"") And Nz(o.Ord1,"")=Nz(p2.Ord1,""))
Da der er brugt left outer joins vil der blive returneret null, hvis kriteriet ikke opfyldes. Da Nz funktionen i SELECT delen sørger for, at der bliver prioriteret mellem de forskellige point-værdier der returneres, sikres det, at din "findes i" fra dinForekomstordPointTabel bliver prioriteret over en evt. samtidig returneret værdi fra den hvor alle ord skal forefindes (dinKombiordPointTabel).
På sin vis kunne det sikkert have været lettere at søge med LIKE i det sammensatte ord, men der er nogle faldgruber, som du undgår ved at teste på alle kombinationerne i stedet, selv om det jo giver en meget lang SQL sætning.
Ved at teste på alle kombinationerne i forespørgslen undgår du også at skulle oprette rækker for alle kombinationerne. Testen på disse kombinationer laves i forespørgslen, hvilket er lettere og sikrere.