0

this is my first question so forgive me if I am doing this wrong. I have been trying to think up a query for the above described problem and I can't seem to get anywhere.

SELECT SUM(slots) AS totals,date 
  FROM 
     (
         SELECT start, end, date, slots 
           FROM 1000_appointments 
          WHERE date > NOW() AND HOUR(end) <= 12 AND employeeID='1000001'
     ) q 
 GROUP BY date HAVING totals < 6

This gets me the days where the total of slots is less than 6 and that is great but I also need all rows for that date and, if at all possible, dates that have no entries in the table. I could possible do the sorting and listing in php but I figured there might be a more elegant way of doing this within mysql.

I hope this describes the problem well enough. The reasoning behind this is that I need to find dates where "slots" are still available ie dates that aren't booked out.

Thank you, for your help in advance.

4

1 回答 1

2

包装将为您获取所有行:

SELECT start, end, a.date, slots, totals 
        FROM 1000_appointments a
        JOIN
        (SELECT SUM(slots) AS totals,date
            FROM 1000_appointments 
            WHERE date > NOW() AND HOUR(end) <= 12 AND employeeID='1000001'
            GROUP BY date HAVING totals < 6
        ) t 
        where a.date = t.date

要得到不在表格中的日期并不容易。它可能需要使用游标。我建议在您的 php 代码中执行此操作。

于 2012-05-28T12:29:17.753 回答