Selecting 2 different type of results

I am making an appointment/booking app and one table holds these and a col in it marks if the appointment was booked from frontend or backand(it is ENUM with these 2 values).

I am trying to build a query here that returns the number of rows that are fronted and the number of rows that are backend.
To get an idea what I am taking about here is a query that counts the “backend” rows for a specified time interval and for a specific business user.

select count(*) from appointments 
WHERE appont_close_time 
BETWEEN '2016-09-01' AND '2016-09-27'
AND bookedfor=(select user_ID from users where concat(name," ",lastname) like 'kostast mpartziotis')
AND apps_origin='backend';

The above will return 5 if 5 are the backend rows.
In essence I want to get 2 numbers one for frontend and one for backend.

I hope I was clear…

GROUP BY is your friend. I would also not use the concat since querying them separately is more efficient as the column indexes can be used - same for the like…

You’re also running a risk that you can have more than one person with that name,

SELECT apps_origin
     , COUNT(*) 
  FROM appointments 
 WHERE appont_close_time BETWEEN '2016-09-01' AND '2016-09-27'
   AND bookedfor = (SELECT user_ID 
                      FROM users 
                     WHERE name = 'kostast'
                       AND lastname =  'mpartziotis')
 GROUP BY apps_origin
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.