Jazz has asked for the wisdom of the Perl Monks concerning the following question:
First, the query:
my $sth = $dbh->prepare( "SELECT * FROM Images LEFT JOIN Regio ON Images.RegioID = Regio.RegioID LEFT JOIN Insula ON Images.InsulaID = Insula.InsulaID LEFT JOIN ImgThemes ON Images.ImageID = ImgThemes.ImageID LEFT JOIN Sources ON Images.SourceID = Sources.SourceID ORDER BY ImageName LIMIT $start,$numrows" );
Now, a simplified version of the table structure:
------- --------- ---------- Regio Images ImgThemes ------- --------- ---------- RegioID >--| ImageID >----< ImageID RegioName |--> RegioID ThemeID <----| ImageName | ------- |--> InsulaID ---------- | Insula | RoomID (etc) Themes | ------- | SourcesID >-| ---------- | InsulaID >-| | ThemeID >----| InsulaName --------- | ThemeName Sources | ThemeCategoryID <-| --------- | | SourceID <--| ---------- | SourceName ThemeCategories | Publishers ---------- | PublishYear ThemeCategoryID >-| CategoryName
The problem is that this yields Cartesian results, creating duplicate values for Image records when there's no match in the Images table for values in the joined ones.
For most of the join tables, the correct value always yields a single result (the name); for "ImgThemes", multiple values are most common. Not every Image is required ImgThemes, but not Regio or Insula.
What is the correct syntax to ensure all images are returned, but not duplicated? This is my first excursion into non-"natural" joins, and the goal is to retrieve each respective name in each of the tables without having to perform separate queries.
Thank you for any help you may be able to offer.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: DBI MySQL Join Question
by graff (Chancellor) on Oct 04, 2009 at 22:19 UTC | |
by Jazz (Curate) on Oct 04, 2009 at 23:54 UTC | |
Re: DBI MySQL Join Question
by zwon (Abbot) on Oct 04, 2009 at 20:52 UTC | |
by Jazz (Curate) on Oct 04, 2009 at 22:44 UTC | |
Re: DBI MySQL Join Question
by EvanCarroll (Chaplain) on Oct 04, 2009 at 22:47 UTC | |
Re: DBI MySQL Join Question
by ELISHEVA (Prior) on Oct 05, 2009 at 04:49 UTC |