I made this kind of thing recently, it's very easy. You have to take advantage of aliases :

(SELECT DISTINCT contacts.id, ..., "Household" as kind FROM Contacts
JOIN ...)
UNION
(SELECT DISTINCT contacts.id, ..., "Company" as kind FROM Contacts
JOIN ...)

Then, "kind" field contains what you need.

Hope this helps ^^

Tom Benson a écrit :
Here is my query:

(SELECT DISTINCT contacts.id, contacts.photo, Title, FirstName, LastName FROM Contacts JOIN Contacts_To_Households ON Contacts.id = Contacts_To_Households.ContactId
JOIN Households ON Contacts_To_Households.HouseholdId =  Households.Id
JOIN Customers ON Households.customerId = Customers.Id
WHERE Customers.id = '<customer.id>')
UNION
(SELECT DISTINCT contacts.id, contacts.photo, Title, FirstName, LastName FROM Contacts JOIN Contacts_To_Departments ON Contacts.id = Contacts_To_Departments.ContactId
JOIN Departments ON Contacts_To_Departments.DepartmentId = Departments.id
JOIN Branches ON Departments.BranchID = Branches.id
JOIN Companies ON Branches.CompanyID = Companies.id
JOIN Customers ON Companies.customerId = Customers.Id
WHERE Customers.id = '<customer.id>')

This query behaves beautifully, but for various reasons I need to know which table combinations a row is compiled from. I'd like to add a 'Virtual Field' to the union, 'Kind', which would have the value "Household" if from the first set and "Company" from the second set.

Any other ways to differentiate the rows would also be welcome.

And before you ask, it is important that all of these values be returned in a single query....

Any ideas??

- Tom
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>




--
Aude FRONT
BSD Concept

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to