Hello,
I need help with a join. I've tried self joins, but I haven't got them to work.
I've decided not to follow previous advice (above) because it resembles binary identification tables that I am trying to get away from. So what I am left with is this: A short version of my database consists of
database
Insects
table names
name
name_id
table face
description
face_id
table antenne
description
antenne_id
table eyes
description
eyes_id
table rel_name_face
name_id
face_id
table rel_name_antenne
name_id
antenne_id
table rel_name_eyes
name_id
eyes_id
table results
description (unique key)
Table "results" is normally empty and only gets populated by choices entered by means of a radio-button form, one choice at a time. The choices consist of the entries found in the columns face.description, antenne.description and eyes.description. The purpose of the MySQL query is to compare results.description with face.description, antenne.description and eyes.description, note any matches and return any fields from names.name that are related to the selections found in results.description . There is a many-to-many relationship between face.description, antenne.description and face_description AND names.name .
With a SELECT...UNION I can get a list of all possible names.name based on results.description.
SELECT n.name FROM names n, results res INNER JOIN rel_name_antenne rel_n_ant ON(n.naam_id=rel_n_ant.naam_id) INNER JOIN antenne ant ON(rel_n_ant.antenne_id=ant.antenne_id) WHERE res.description = ant.description
UNION SELECT name FROM names n, results res INNER JOIN rel_naam_face rel_n_face ON(n.naam_id=rel_n_face.naam_id) INNER JOIN face ON(rel_n_face.face_id=face.face_id) WHERE res.description = face.description
UNION SELECT name FROM names n, results res INNER JOIN rel_naam_eyes ON(n.name_id=rel_n_eyes.name_id) INNER JOIN eyes ON(rel_n_eyes.eyes_id=eyes.eyes_id) WHERE res.description = eyes.description
But I want the ever decreasing intersection set of names.name based on all the fields in results.description . Ideally res.description(id=1) is related to names.name (id=1),(id=5) and (id=6); res.description (id=2) is related to names.name (id=1) and (id=3) and res.description (id=3) is related to res.description (id=1) and (id=4). The intersection set is res.description (id=1)
I've tried variations on SELECT...UNION, self joins and WHEN EXISTS(SELECT...) but nothing quite returns what I'm loking for. Any suggestions?
-Roy