2011-01-20 188 views
0

我有两个表,其中一个计划另一个特定时间段的可用约会列表。我把时间基于日期(第一查询)的一周中的一天子查询比较不返回结果

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

上述工作正常。

我从哪里获得任命的全部金额的日期和具体时间

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

在我来说,现在我在上2011/01/17在同一时间,这是两项任命第二查询准确地返回使用上述。

我在store_schedule中有一个名为concurrent_times的列来确定有多少约会可以在store_appointment中共享同一个store_schedule_id。以下是我的组合查询。

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_schedule_id = b.id 
and apt_date = '2011-01-17') < concurrent_appointments 

由于某种原因,此查询返回零结果。任何人都可以看到我做错了什么?每个查询分解工作正常。

回答

0

我是个白痴:(。我误读了我自己的问题。我不需要第二个链接store_schedule。

以下是正确的查询

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