![](/img/trans.png)
[英]Getting count of rows from one table where at least one related item from another table matches criteria
[英]Count rows in a table according to criteria in another table
我有兩個表,“預訂”和“體育設施”。 用戶可以在給定的時間內預訂給定的體育設施(每個設施/小時均具有唯一的ID,例如“ 9:00”的“游泳池”將具有與“ 10:00”不同的ID,並且在不同的日子)。
預訂包含以下幾列... [ID(PK),ID_FACILITY,ID_USER,DATE,HOUR,PAID,PAYMENT_METHOD]
設施包括以下幾列... [ID(PK),名稱,州,PRICEPERHOUR,DATE,HOUR]
我的問題是我不知道如何比較這兩個表之間的值。 我想對“ Booking”表中的條目進行計數,但要檢查ID_FACILITY值是否等於給定值,例如“游泳池”,還要檢查小時數是否是“ 9:00”。
我到目前為止得到的錯誤查詢是這個...
select count(*) as totalbookingcriteria from public.booking, public.facility where hour = '9:00' and name = 'Swimming Pool'
您需要的只是表的INNER JOIN
,因此
select count(*) as totalbookingcriteria
from public.booking b
INNER JOIN public.facility f
ON ( b.ID_FACILITY = f.ID )
where f.hour = '9:00'
and f.name = 'Swimming Pool'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.