简体   繁体   中英

Count rows in a table according to criteria in another table

I have two tables, "Booking" and "Sports facility". A user can book a given sports facility in a given hour (each facility/hour has a unique ID, like for example "Swimming Pool" at "9:00" will have a different ID than at "10:00", and also a different one in a different day).

Booking has the following columns... [ ID (PK), ID_FACILITY, ID_USER, DATE, HOUR, PAID, PAYMENT_METHOD ]

Facility has the following columns... [ ID (PK), NAME, STATE, PRICEPERHOUR, DATE, HOUR ]

The problem that I have is that I don't know how to compare the values between those two tables. I would like to count the entries in the "Booking" table but checking that the ID_FACILITY value is equal to a given one, like "Swimming Pool" for example, and also check that the hour is "9:00" for example.

The wrong query I got so far is this one...

select count(*) as totalbookingcriteria from public.booking, public.facility where hour = '9:00' and name = 'Swimming Pool'

What you need is just an INNER JOIN for your tables, so

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' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM