MYSQL and condition

Asked

Viewed 32 times

0

I need to make a select in my same database is below:

select 
* 
from tbl_frete f 
join tbl_transportador t on f.idtransportador = t.idtransportador 
join tbl_cliente c on f.idcliente = c.idcliente 
join tbl_situacaofrete s on f.situacaofrete = s.idsituacao 
where f.idcliente = 1 
and f.situacaofrete = 0 
and f.situacaofrete = 1 
order by f.idfrete

but returns nothing, and when I do select so:

select 
* 
from tbl_frete f 
join tbl_transportador t on f.idtransportador = t.idtransportador 
join tbl_cliente c on f.idcliente = c.idcliente 
join tbl_situacaofrete s on f.situacaofrete = s.idsituacao 
where f.idcliente = 1  
order by f.idfrete

returns the data but not as I wanted, since it does not return the freight with situation 1

How do I do this AND?

  • 1

    On the first command, you’re saying you want to f.situacaofrete = 0 and f.situacaofrete = 1. Try to take out the first part of the condition (f.situacaofrete = 0)

  • @Leonardopessoa but then those who have situation = 0 do not appear

  • there are situations from 0 to 3, I want 0 and 1 to appear

  • 1

    example has quite down there, I hope it helps you

1 answer

2


John, as Leonardo said, you put on Where the condition of the record having two values at the same time, it will always return empty. Just take one of the conditions, or change the operator.

Removing one of the conditions:

select 
* 
from tbl_frete f 
join tbl_transportador t on f.idtransportador = t.idtransportador 
join tbl_cliente c on f.idcliente = c.idcliente 
join tbl_situacaofrete s on f.situacaofrete = s.idsituacao 
where f.idcliente = 1 
and f.situacaofrete = 1 
order by f.idfrete;

Changing the operator:

select 
* 
from tbl_frete f 
join tbl_transportador t on f.idtransportador = t.idtransportador 
join tbl_cliente c on f.idcliente = c.idcliente 
join tbl_situacaofrete s on f.situacaofrete = s.idsituacao 
where f.idcliente = 1 
and (f.situacaofrete = 0 
OR f.situacaofrete = 1) 
order by f.idfrete;

Or use the in

select 
* 
from tbl_frete f 
join tbl_transportador t on f.idtransportador = t.idtransportador 
join tbl_cliente c on f.idcliente = c.idcliente 
join tbl_situacaofrete s on f.situacaofrete = s.idsituacao 
where f.idcliente = 1 
and f.situacaofrete in (0,1) 
order by f.idfrete;

Or, delete unwanted results:

select 
* 
from tbl_frete f 
join tbl_transportador t on f.idtransportador = t.idtransportador 
join tbl_cliente c on f.idcliente = c.idcliente 
join tbl_situacaofrete s on f.situacaofrete = s.idsituacao 
where f.idcliente = 1 
and f.situacaofrete != 2 
order by f.idfrete;
  • 1

    thanks!!! I needed to use the in!

Browser other questions tagged

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