At select distinct hvor ALLE rækker i en anden tabel er med
Følgende SQL-sætning skal modificeres:
select distinct tblAvisTilbud.tilbudid from tblAvisTilbud, tblavistilbudindhold, tblavisannoncesektion, tblavissektionpostnummer where tblavistilbudindhold.tilbudid = tblavistilbud.tilbudid and tblavisannoncesektion.annonceid = tblavistilbudindhold.annonceid and tblAvisTilbud.bitFastTilbud=0 and tblAvisTilbud.bitDeaktiveret=0 and tblavisannoncesektion.sektionid=tblavissektionpostnummer.sektionid and tblavissektionpostnummer.intpostnummer = 2000
Mit problem er, at jeg KUN vil have rækker fra tblAvisTilbud hvor ALLE rækker i tblavissektionpostnummer.intpostnummer er lig med 2000... lige nu får jeg nemlig et resultat bare der er en enkelt række i tblavissektionpostnummer som er lig 2000.
Jeg tror lidt at løsningen er noget med group by :)
Hvis I har brug for at få forklaret noget af den utilstrækkelige SQL-sætning, skal I bare sige til... men jeg vurderede den til at være rimelig simpel :)
Uuuh... ja... jeg glemte at sige, at tblavissektionpostnummer også indeholder ANDRE rækker med et intpostnummer til pågældende sektionID :( Dvs. at ved at skrive AND NOT IN.. ja, så får man udelukket rækker på et forkert grundlag :(
Ydermere ligger der et stort problem i at tblavissektionpostnummer er en meget stor tabel, så det er voldtsomt begrænset, hvor meget man kan lave uden at lægge serveren ned.
Please kig på det. Som sagt skal der gøres følgende: Hvis samtlige avisannoncer i et tilbud har mindst en sektion som har mindst et rigtigt postnummer, så skal tilbudet selectes..
Jeg forstår ikke helt, skal der kun være postnummer 2000 i tblavissektionpostnummer førend at tilbuddet skal vises?
Eller skal det forståes som at alle sektionsid'er som findes i tblAvisSektionPostnummer med postnummer 2000 også skal findes i tblAvisTilbud?? - Og hvis bare en sektionID mangler skal intet vises?
Det er korrekt forstået, Janus. Hvis et tilbud indeholder bare EEN sektion, som ikke er tilknyttet det aktuelle postnummer, skal tilbudet ikke vises. Ved godt, det er en gammel tråd, men jeg har aldrig fået løst problemet =)
I think it can be done maybe like this... I'll try back @ work...
SELECT tblAvisTilbudIndhold.tilbudid FROM tblAvisTilbudIndhold, tblAvisAnnonce, tblAvisAnnonceSektion, tblAvisSektionPostnummer WHERE tblAvisTilbudIndhold.annonceid=tblAvisAnnonceSektion.annonceid and tblAvisAnnonceSektion.sektionid=tblAvisSektionPostnummer and tblAvisSektionPostnummer.intPostnummer=2000 and tblAvisTilbudIndhold.tilbudid=1 GROUP BY tblAvisTilbudIndhold.tilbudid HAVING count(tilbudid)= (SELECT count(sektionid) FROM tblAvisAnnonceSektion, tblAvisTilbudIndhold WHERE tblAvisAnnonceSektion.annonceID=tblAvisTilbudIndhold.annonceid))
What it basicly does is use the HAVING method and a subselect to make sure that the number of offers that correspond to the postal code also correspond to the actual number of ads included in the offer.
any chance of you exporting the SQL script for the tables and maybe also exporting some data? Its a little difficult tesing anything without any data, and when we dont know what data to put in the tables to get it to work.
Nah... i figured it was useless to include tblAvisTilbud in the example - it's just one simple LEFT JOIN away =) But I shall definately test the query tomorrow. When I'm done testing I'll let you know if it succeeded. The whole trick is to campare the HAVING count(tilbudid) with a sub query... the sub query by the way has an err and is supposed to look like this:
(SELECT count(sektionid) FROM tblAvisAnnonceSektion, tblAvisTilbudIndhold WHERE tblAvisAnnonceSektion.annonceID=tblAvisTilbudIndhold.annonceid and tblAvisTilbudIndhold.tilbudID=1)
The "tilbudID" is legal to hard code. But in order to get ALL offers we should of cause remove the "tilbudID=1" demand and just get the whole load of offers =)
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.