MichaelHooker
Programmer
Please forgive what's probably a basic query for all of you. And I don't know the correct terminology yet. I need to retrieve data from four tables into one php "$result" with one MySQL query. The tables are configured as follows:
Images
ImageID (primary key & the only unique field)
Serial
Photographer (all photographers are listed once in the Posters.Photographer field, ie many to one)
Operator (only some operators are listed in the Operators.Operator field, not all, ie many to the occasional one)
Subject (all subjects are listed once in the Subject.Subject field, ie many to one)
Operators
Operator (primary key)
OpField1
OpField2
etc
Photographers
Photographer (primary key)
PhoField1
PhoField2
etc
Subjects
Subjects (primary key)
SubField1
SubField2
etc
The task: I need to select ALL images from the Images table meeting criteria set by conditionals on fields in that table, eg 'where Serial = "ABCDE"'. For each hit I need to fetch all the relevant fields from all four tables, relating to the Image, the Operator (where they exist), Photographer and Subject. Obviously I don't need to retrieve the linking fields twice.
"The answer's going to be a left join" I hear you thinking - well, something like that. Because a straight join between Images.Operator and Operators.Operator results in the correct image data not being retrieved when the particular operator does not appear in the Operators table (there are good reasons for this, the details for many operators simply don't exist or can't be traced).
So, either I add all the Operators that occur in the Images table to the Operators table, even though there is nothing to put in the detail fields, or I need a query that successfully combines all the correct kinds of joins. I've found a few likely-looking examples to follow on the internet and in books, but all sorts of unexpected things happened when I tried them. I've read so much about joins that my head's ing. And I don't learn so fast these days (no hair left to keep the few remaining brain cells warmed up). I have a suspicion that it's actually less complicated than the guidance makes it out to be...
So I'd be very grateful if someone could rescue me and explain how I put together a query which does what I said 3 paragraphs ago, which I hope makes sense to you. I promise to put my woolly hat on and try to make it sink in.
Thanks for listening,
Michael Hooker
Images
ImageID (primary key & the only unique field)
Serial
Photographer (all photographers are listed once in the Posters.Photographer field, ie many to one)
Operator (only some operators are listed in the Operators.Operator field, not all, ie many to the occasional one)
Subject (all subjects are listed once in the Subject.Subject field, ie many to one)
Operators
Operator (primary key)
OpField1
OpField2
etc
Photographers
Photographer (primary key)
PhoField1
PhoField2
etc
Subjects
Subjects (primary key)
SubField1
SubField2
etc
The task: I need to select ALL images from the Images table meeting criteria set by conditionals on fields in that table, eg 'where Serial = "ABCDE"'. For each hit I need to fetch all the relevant fields from all four tables, relating to the Image, the Operator (where they exist), Photographer and Subject. Obviously I don't need to retrieve the linking fields twice.
"The answer's going to be a left join" I hear you thinking - well, something like that. Because a straight join between Images.Operator and Operators.Operator results in the correct image data not being retrieved when the particular operator does not appear in the Operators table (there are good reasons for this, the details for many operators simply don't exist or can't be traced).
So, either I add all the Operators that occur in the Images table to the Operators table, even though there is nothing to put in the detail fields, or I need a query that successfully combines all the correct kinds of joins. I've found a few likely-looking examples to follow on the internet and in books, but all sorts of unexpected things happened when I tried them. I've read so much about joins that my head's ing. And I don't learn so fast these days (no hair left to keep the few remaining brain cells warmed up). I have a suspicion that it's actually less complicated than the guidance makes it out to be...
So I'd be very grateful if someone could rescue me and explain how I put together a query which does what I said 3 paragraphs ago, which I hope makes sense to you. I promise to put my woolly hat on and try to make it sink in.
Thanks for listening,
Michael Hooker