andreaem andreaem - 3 months ago 7
MySQL Question

MySQL SELECT where there are two clauses and the second can be three values

I want to count rows in a table where there is correspondence with username and status. The status can be 1,2 or 3.

| ID | username | status |
--------------------------
| 1 | john | 1 |
| 2 | john | 2 |
| 3 | john | 3 |
| 4 | john | 4 |
| 5 | jenna | 2 |
--------------------------


There are 4 entries for user John, but the last has status 4 so I don't need to count it.

So I made this query

SELECT count(*) FROM orders WHERE username = 'john' AND status = 1 OR 2 OR 3


but this counts every row even if there are records with a username and status not in the query.

Answer

Try this

 SELECT count(*) FROM orders WHERE username = 'john' AND status IN ( 1,2, 3)
Comments