dimanche 26 avril 2015

MySQL LEFT JOIN a different table based on a field value

I know similar questions have been presented a few times, but it seems these all related to the field value changing based on a specific field value. I actually want to join a completely different table based on the the field value of each record.

I think this is possible with a single query, but since the join table could change every record I'm not sure.

To clarify I have four tables; I am trying to join three of them in a single query. However the third table could be one of two tables depending on the value in each records field.

I would imagine it would look something like this, but its not working.

SELECT crm_deal . * , crm_deal_step.sort_order, COALESCE( CONCAT_WS(' ',crm_lead.first_name, crm_lead.last_name),CONCAT_WS(' ',customer.firstname, customer.lastname) ) AS contact_full_name
FROM  `crm_deal`
IF( crm_deal.deal_for = 'lead', LEFT JOIN crm_lead ON crm_deal.deal_for_id = crm_lead.lead_id, LEFT JOIN customer ON crm_deal.deal_for_id = customer.customer_id)
LEFT JOIN crm_deal_step ON crm_deal.deal_step_id = crm_deal_step.deal_step_id

Am I close? Is it possible or do I need to do a php loop and run a second query for every record?

Aucun commentaire:

Enregistrer un commentaire