Help make a complex query.

In general, there are 3 tables. Contacts, Transactions and Tasks

There are 2 entities in contacts (Customers and companies).

Клиент связан с компанией (не обязательно) Клиент связан со сделкой (не обязательно) Компания связана со сделкой (не обязательно) Контакт связан с задачей (не обязательно) Компания связана с задачей(не обязательно) Сделка связана с задачей (не обязательно) 

I need to select those contacts that have no task for any of the entities. Suppose if a client does not have a task, but the transaction in which he is having a task, then you do not need to display this contact. If the client has a company that has neither a deal nor a task, then you need to withdraw this company.

Wrote like this, but displays incorrectly.

 SELECT c.sid FROM `contacts` c left JOIN `link_contacts_leads` lcl ON c.id = lcl.contact_id left JOIN `leads` l ON l.id = lcl.lead_id left JOIN tasks t ON t.el_id = c.sid or t.el_id = l.sid WHERE t.el_id is NULL Таблица contacts id,sid,type,company_id //Если company_id не null, значит у клиента есть компания. type это клиент или компания Таблица link_contacts_leads id,lead_id,contact_id Таблица leads id Таблица tasks id, el_id //el_id это id контакта или сделки 
  • And why do you write something about the company_id field and do not use it in your request. it is not at all clear why it is and what it is connected with. - Mike
  • If company_id is not null, then the client has a company. Related to contascts.sid - Radik Kamalov
  • Those. The contacts table is related to itself. company_id can point to another sid, for which company_id can lead somewhere else. we receive a recursion which in MySQL by normal means is not developed at all. I do not think that someone will answer you the question in this formulation. because nothing is clear. no samples of input data in the tables and the desired result. It is best to give an example of a database with data on some sqlfiddle.com - Mike

0