Hello,
I am doing a self join two times in one query, and I need to show all records even if one of the fields is not existent. Hard to explain, but here's the query.
SELECT mainTitle.pk_TitleID, mainTitle.content, altTitle.content, physTitle.content, mainTitle.fk_ObjectID
FROM Title AS mainTitle, Title AS altTitle, Title AS physTitle, Object AS o
WHERE mainTitle.fk_ObjectID=o.pk_ObjectID
AND mainTitle.fk_TitleTypeID=1
AND altTitle.fk_ObjectID=o.pk_ObjectID
AND altTitle.fk_TitleTypeID=2
AND physTitle.fk_ObjectID=o.pk_ObjectID
AND physTitle.fk_TitleTypeID=3;
In the FROM section you can see the table "Title" is aliased three times - mainTitle, altTitle, and PhysTitle and they reference the primary key in the "Object" table. The "Title" table contains approx. three times the entries that the "Object" table does because of the foreign key fk_TitleTypeID that references the "TitleType" table which contains 1 mainTitle, 2 altTitle, 3 physTitle.
The problem is, if there is a mainTitle and a physTitle but no altTitle for an object, the record is not listed at all in the selected records. I want it to show the item with the mainTitle and physTitle, and just leave the altTitle blank.
I will be happy to provide more info if you need it, but I don't want this post to be more confusing than it already is. I would appreciate any help on getting this query working properly.
Thanks, Fadad
I am doing a self join two times in one query, and I need to show all records even if one of the fields is not existent. Hard to explain, but here's the query.
SELECT mainTitle.pk_TitleID, mainTitle.content, altTitle.content, physTitle.content, mainTitle.fk_ObjectID
FROM Title AS mainTitle, Title AS altTitle, Title AS physTitle, Object AS o
WHERE mainTitle.fk_ObjectID=o.pk_ObjectID
AND mainTitle.fk_TitleTypeID=1
AND altTitle.fk_ObjectID=o.pk_ObjectID
AND altTitle.fk_TitleTypeID=2
AND physTitle.fk_ObjectID=o.pk_ObjectID
AND physTitle.fk_TitleTypeID=3;
In the FROM section you can see the table "Title" is aliased three times - mainTitle, altTitle, and PhysTitle and they reference the primary key in the "Object" table. The "Title" table contains approx. three times the entries that the "Object" table does because of the foreign key fk_TitleTypeID that references the "TitleType" table which contains 1 mainTitle, 2 altTitle, 3 physTitle.
The problem is, if there is a mainTitle and a physTitle but no altTitle for an object, the record is not listed at all in the selected records. I want it to show the item with the mainTitle and physTitle, and just leave the altTitle blank.
I will be happy to provide more info if you need it, but I don't want this post to be more confusing than it already is. I would appreciate any help on getting this query working properly.
Thanks, Fadad