如何修复此SQL查询?

时间:2015-07-29 14:01:58

标签: sql database mysqli

我尝试编写一个检查数据库中预留位置的查询并检查餐馆的容量。查询是:

Select COUNT(r.capacity<rs.no_people) As people;

SELECT `r.restaurant_id`
, `r.restaurant_name`
, `r.capacity`
, `rs.no_of_people`
, `rs.date`
, `rs.start_time`
, `rs.end_time`
FROM `restaurant`AS r,reservation AS rs
Where r.restaurant_id=rs.restaurant_id

enter image description here

2 个答案:

答案 0 :(得分:0)

SELECT `r.restaurant_id`, `r.restaurant_name`, `r.capacity`,    
`rs.no_of_people`, `rs.date`, `rs.start_time`, `rs.end_time`
, (`r.capacity` - `rs.no_of_people`) as new_column
FROM `restaurant` r JOIN reservation rs
ON `r.restaurant_id` = `rs.restaurant_id`

首先,这两个表必须是join。然后你得到capacityno.of.people的差异。

答案 1 :(得分:0)

假设您只想在特定时间间隔内检查可用座位:

SELECT r.*, r.capacity - SUM(rs.people) AS available
FROM restaurant r
LEFT JOIN reservation rs ON r.restaurant_id = rs.restaurant_id
                        AND <date> = rs.date
                        AND <start_time> < rs.end_time
                        AND <end_time> > rs.start_time
GROUP BY r.restaurant_id

<date><start_time><end_time>替换为相关值。如果预订可能跨越午夜/不同日期,您可能需要调整表格设计和查询。