php query filter mysql

3

BD Structure

Companies

 -----------------------------------
 | uid   |   nome    |  endereco   |
 | 1     | empresa01 |  endereço01 |
 | 2     | empresa02 |  endereço02 |
 -----------------------------------

Products

 -------------------------------------------
 | id |  produto  |  valor | empresa | tags|
 | 1  |  produto1 |   1    |    1    |   a |
 | 2  |  produto1 |   1    |    2    |   a | 
 -------------------------------------------  

If the user searches for "produto1" or tag "a" it will return the products of the two companies. I want to put a filter if the user wants to filter only by the products of a company, the query I tried is this one below but it did not work the way I wanted! When filtering by company 1 returns certain but filtering by company 2 behind the two companies.

$filtrar = mysql_query("select * from produtos inner join empresas
on  uid = empresa where empresa = '$filtro' and  produto like '%$busca%'
or tags like '%$busca%' ");

Goal: Search for "produto1" filtering empresa = 2 should return only the company product 2 '.

    
asked by anonymous 15.11.2016 / 14:39

2 answers

2

The or must be enclosed in parentheses so that it is isolated from other instructions:

select *
from produtos
inner join empresas
on  uid = empresa
where empresa = '$filtro'
and  (produto like '%$busca%'
or tags like '%$busca%')
    
15.11.2016 / 14:48
0
$filtrar = mysql_query("select * from produtos inner join empresas
on  uid = empresa where empresa = '$filtro' and  (produto like '%$busca%'
or tags like '%$busca%') ");
    
15.11.2016 / 14:43