Search multiple Mysql tables

Asked

Viewed 561 times

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 you have, returns the Results... But there always returns zero... What will be? Someone help me?

Table structure:

inserir a descrição da imagem aqui

  • The research is OR or AND?

  • The query is OR, if it is within the table, any of them.. returns, if it does not return empty... but the search field is one.

3 answers

2


Analyzing your SQL you can see that it wasn’t very clear to you how to make relationships in JOINS, recommend you read here: What is the difference between INNER JOIN and OUTER JOIN?

Leaving for your trouble, I’d 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

This way is linked all tables by your id_cliente ensuring data integrity, if any of these tables are not required in your result you can exchange INNER for 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}%'
  • I just traded the id_client for id, which is the right one and also instead of using INNER, I switched to LEFT. It worked! Thanks guys.

  • In fact, corrected.

0

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

In the Inners you have to use the condition on to determine when there will be the junction, in case I put that each table has the id_client there I make the junction with the client using the id and your LIKE I put in the condition WHERE

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 executions!

  • It didn’t work either, because of the search tag... I need to elaborate the SQL if I find some result among the tables, return, regardless of which table has been.

  • But what is this result? is a Count? is a list of columns with several rows..., I don’t understand your logic

  • I got it now, just a minute

  • Ready now should work

  • 1

    worked well... I used the example of Maico, I thank the help

  • 1

    I saw now his answer :) is exactly that.

Show 1 more comment

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%'; 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.