Query SQL listing shopkeepers by id and status

Asked

Viewed 33 times

0

Personal I have a table where I list purchases of customers, in this table I have information as the id of the shopkeeper and the status of the order, this status goes from 0 to 6, however I would like to display only when the status is up to 5, I am trying this way

SELECT * FROM tbl_compras WHERE loja_id="& lojista_id &" OR status=0 OR status=1 OR status=2 OR status=3 OR status=4 OR status=5 ORDER BY status ASC LIMIT 20

But end up returning me all the results of other shopkeepers and I just want a specific shopkeeper.

Where am I going wrong?

  • You need to encapsulate between parentheses the two conditions.

  • The first OR seems to be wrong, use the IN < ...loja_id="& lojista_id &" and status in (1,2,3,4,5) ... >

2 answers

2


You are selecting everyone who has id = $id OR any other status, do

loja_id = "id do lojista " AND status < 6

Logic error, is using or instead of "and"

If you need to use several OR like you did, use parentheses:

loja_id = "id do lojista " AND (status = 5 OR status = 4...) //por exemplo
  • 1

    understood! thank you :D

0

Use the clause loja_id = "shopkeeper id " AND status <= 5

This way will bring only all logidta IDS ranging from 0 to 5 If you want to take an excellent SQL course I leave below the link of my course portal www.fpw.com.br/ead There they have the most complete SQL course that you find on the internet

Browser other questions tagged

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