Hi All,
Looking for some assistance with a SQL query.
I am trying to get my query to only show me results where there isn’t a status of 1 and 4 together. Hopefully I have explained below.
Users view the orders and apply for them. When they apply for the order, we ‘INSERT into tbl_applications’ with the status of 1, so I can see who applied for it by seeing the list of applications where Status = ‘1’ If a user doesn’t apply for it, we don’t see anything in the applications table.
When we select the user we are offering this to, we offer it to them and the status changes to 2. If they decline, the status is updated to 3. When the user accepts we change the status to 4.
At the moment, I can pull down the list of orders where there are no applications and I can pull down when there are applicants.
What I am struggling to do is, when the status of 4 is associated with the orderID, we don’t display this as available on the screen.
I am using the following query at the moment to pull through all the available orders.
SELECT * FROM tbl_orders
LEFT JOIN tbl_applications on tbl_applications.orderID = tbl_orders.orderID
WHERE (tbl_applications.Status IS NULL OR applications.Status = ‘1’ OR applications.Status = ‘2’ OR applications.Status = ‘3’)
group by tbl_orders.orderID
Schema:
tbl_users tbl_applications tbl_orders
+---------+----------+ +----------------+----------+---------+--------+ +----------+------------+
| userID | userFN | | applicationID | orderID | userID | Status | | orderID | dateFor |
+---------+----------+ +----------------+----------+---------+--------+ +----------+------------+
| 1 | Jane | | 1 | 2 | 2 | 1 | | 1 | 2019-06-01 |
| 2 | Lisa | | 2 | 2 | 1 | 2 | | 2 | 2019-07-01 |
| 3 | Scott | | 3 | 3 | 1 | 1 | | 3 | 2019-07-15 |
| 4 | Peter | | 4 | 4 | 1 | 1 | | 4 | 2019-07-25 |
| 5 | Julie | | 5 | 6 | 1 | 1 | | 5 | 2019-08-01 |
| 6 | Frank | | 6 | 1 | 1 | 1 | | 6 | 2019-01-02 |
+---------+----------+ +----------------+----------+---------+--------+ +----------+------------+