1

I have a table that looks as follows service:

id | service           | status | multicall | date_created         |
--------------------------------------------------------------------
10 | Cleaning          |    1   |      0    |  2016-09-20 19:11:01 |    
11 | Gardening         |    1   |      0    |  2016-09-20 19:12:07 |
12 | Dishes            |    1   |      1    |  2016-09-20 19:14:28 |    
13 | Take out rubbish  |    1   |      1    |  2016-09-20 19:14:28 |    

This a a table that users can can select service from that they want someone to do. Once the service is finished, the user history gets saved in a different table user_service_hist:

id | user_id | service_id | date_created         |
--------------------------------------------------
1  |    1    |     10     |  2016-11-25 10:49:05
2  |    1    |     11     |  2016-11-27 23:58:46
3  |    1    |     12     |  2016-11-28 00:01:36
4  |    1    |     13     |  2016-11-28 12:07:17

The user can only select each service once EXCEPT the service marked with a multicall column value of 1.

When I call the list of available service I use the follwing query:

SELECT s.id, s.service, s.date
from service s WHERE not exists (Select 1 from user_service_hist ush    
where ush.service_id = s.id AND ush.user_id = 1)

This query selects zero rows as I the user has completed all services. I would like to display service nr. 12 and 13 always as these have a multicall column value of 1, regardless of whether it was completed or not. Could someone tell me how to modify my query?

2
  • 1
    Wouldn't a simple "OR s.multicall = 1" suffice? Commented Dec 3, 2016 at 13:55
  • @Stefan I think hank is right. isn't? Commented Dec 3, 2016 at 13:59

2 Answers 2

1

You could combine the query with an union for multicall

  SELECT s.id, s.service, s.date
  from service s WHERE not exists (Select 1 from user_service_hist ush    
  where ush.service_id = s.id AND ush.user_id = 1)
  union 
  SELECT s.id, s.service, s.date
  from service s WHERE multicall  = 1
Sign up to request clarification or add additional context in comments.

Comments

0

You have two conditions for available services:

  • The user has not used the service previously.
  • The service is multi-call.

You just need to include both in the query. The second goes in the where clause as an additional condition:

select s.*
from service s 
where not exists (Select 1
                  from user_service_hist ush    
                  where ush.service_id = s.id and ush.user_id = 1
                 ) or
      s.multicall = 1;

1 Comment

Hi I think that this should work. Unfortunately I have other conditions in my first WHERE clause. I did not mention these to simplify the query. When I try to run the new query with the OR clause at the end my server runs into an inifinite loop and says "Loading...." in phpMyAdmin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.