开发者

Subquery Comparison not returning results

I have two tables, one a schedule the other a list of available appointments for specific timeblocks. I take the times for a day of the week based on a date (First query)

select * from store_schedule where schedule_day = DATE_FORMAT('2011-01-17', '%a')

The above works fine.

I have a second query where I get the total amount of appointments for a date and specific time

SELECT count(*) from store_appointment a, store_schedule b 
where a.store_schedule_id = b.id and apt_date = '2011-01-17'

In my case now I have two appointments at the same time on 2011/01/17 which is accurately returned using the above.

I have a column called concurrent_times in my store_schedule to determine how many appointments can share the same store_schedule_id in store_appointment. Below is my combined query.

select * from store_schedule where 
schedule_day = DATE_FORMAT('2011-01-17', '%a') AND 
(SELECT count(*) from store_appointment a, store_schedule b 
where a.store_sc开发者_C百科hedule_id = b.id 
and apt_date = '2011-01-17') < concurrent_appointments

This query returns ZERO results for some reason. Can anyone see what I am doing wrong? Each query broken down works fine.


I'm an idiot :(. I misread my own queries. I don't need a second link to store_schedule.

Below is the correct query

select * 
from store_schedule aa 
where schedule_day = DATE_FORMAT('2011-01-17', '%a')  
  and ((select count(a.id) as countTotal 
        from store_appointment a 
        where a.store_schedule_id = aa.id 
          and apt_date = '2011-01-17') + 0) < concurrent_appointments 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜