Query filter php mysql

Asked

Viewed 848 times

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" 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 below but it did not work as wanted! When filter by enterprise 1 returns right but when filter by enterprise 2 brings 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%' ");

Objective: Search for "produto1" filtering empresa = 2 should return only the product of company 2`.

2 answers

2

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

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

0

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

Browser other questions tagged

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