Search in multiple tables MySQL

1

I have the following SQL

SELECT * FROM 
cliente as c 
INNER JOIN cliente_contato as ct ON ct.nome LIKE '%{$pesquisa}%' OR ct.telefone LIKE '%{$pesquisa}%'
JOIN cliente_ip as cip ON cip.detalhes LIKE '%{$pesquisa}%'
INNER JOIN cliente_mac as mac ON mac.detalhes LIKE '%{$pesquisa}%'
INNER JOIN cliente_pppoe as pppoe ON pppoe.usuario LIKE '%{$pesquisa}%'
WHERE c.nome LIKE '%{$pesquisa}%'

I would like to search in several tables the same TAG, if it has, returns the results ... But there it always returns zero ... What will it be? Someone help me?

Table Structure:

    
asked by anonymous 01.07.2015 / 19:03

3 answers

1

Looking at your SQL to see that it was not very clear to you how to make relationships in JOINS , I recommend you read here: What is the difference between INNER JOIN and OUTER JOIN? p>

Leaving for your problem, I would do something like this:

SELECT * FROM cliente as c 
INNER JOIN cliente_contato as ct ON ct.id_cliente = c.id
INNER JOIN cliente_ip as cip ON cip.id_cliente = c.id
INNER JOIN cliente_mac as mac ON mac.id_cliente = c.id
INNER JOIN cliente_pppoe as pppoe ON pppoe.id_cliente = c.id

In this way all tables are linked by their id_cliente , guaranteeing data integrity, if any of these tables is not mandatory in your result you can change INNER by LEFT .

Leaving for your research:

WHERE ct.nome LIKE '%{$pesquisa}%' 
   OR ct.telefone LIKE '%{$pesquisa}%'
   OR cip.detalhes LIKE '%{$pesquisa}%'
   OR mac.detalhes LIKE '%{$pesquisa}%'
   OR pppoe.usuario LIKE '%{$pesquisa}%'
   OR c.nome LIKE '%{$pesquisa}%'

That way it will bring the result if only one of the conditions meet.

Final Code

SELECT * FROM cliente as c 
INNER JOIN cliente_contato as ct ON ct.id_cliente = c.id
INNER JOIN cliente_ip as cip ON cip.id_cliente = c.id
INNER JOIN cliente_mac as mac ON mac.id_cliente = c.id
INNER JOIN cliente_pppoe as pppoe ON pppoe.id_cliente = c.id
WHERE ct.nome LIKE '%{$pesquisa}%' 
   OR ct.telefone LIKE '%{$pesquisa}%'
   OR cip.detalhes LIKE '%{$pesquisa}%'
   OR mac.detalhes LIKE '%{$pesquisa}%'
   OR pppoe.usuario LIKE '%{$pesquisa}%'
   OR c.nome LIKE '%{$pesquisa}%'
    
01.07.2015 / 19:28
0
  

With the use of inner join if one of the tables does not have the result it will not return any line. Try LEFT JOIN.

In Inner join you have to use the on condition to determine when there is going to be a join, in case I put that each table has the client_id, I do the join with the client using the id and its LIKE I put in the WHERE condition

SELECT * FROM 
cliente as c 
LEFT JOIN cliente_contato as ct ON ct.id_cliente = c.id
LEFT JOIN cliente_ip as cip ON cip.id_cliente = c.id
LEFT JOIN cliente_mac as mac ON mac.id_cliente = c.id
LEFT JOIN cliente_pppoe as pppoe ON pppoe.id_cliente = c.id
WHERE c.nome LIKE '%{$pesquisa}%' OR ct.nome LIKE '%{$pesquisa}%' OR ct.telefone LIKE '%{$pesquisa}%' OR cip.detalhes LIKE '%{$pesquisa}%' OR mac.detalhes LIKE '%{$pesquisa}%' OR pppoe.usuario LIKE '%{$pesquisa}%'

The right thing would be for you to do different queries and do different runs!

    
01.07.2015 / 19:24
0

I would do it this way:


SELECT c.*, ct.*, cip.*, mac.*, pppoe.* 
FROM cliente c 
LEFT JOIN cliente_contato ct ON (ct.id_cliente=c.id)
LEFT JOIN cliente_ip cip ON (cip.id_cliente=cip.id)
LEFT JOIN cliente_mac mac ON (mac.id_cliente=cip.id)
LEFT JOIN cliente_pppoe pppoe ON (pppoe.id_cliente=cip.id)
WHERE c.nome LIKE '%$pesquisa%'
OR    ct.nome LIKE '%$pesquisa%' 
OR    ct.telefone LIKE '%$pesquisa%'
OR    cip.detalhes LIKE '%$pesquisa%'
OR    mac.detalhes LIKE '%$pesquisa%'
OR    pppoe.usuario LIKE '%$pesquisa%'; 

    
02.07.2015 / 15:17