Matthis Kohli
Matthis Kohli

Reputation: 1995

How to use count for each row PostgreSQL

I have a table in pgadmin for postgres which holds two private keys from two other tables. Its about modeling a hospital database in PostgreSQL. I created two tables "bed" and "room". I save the private keys from this tables in a table called "bed_rooms" which will give information about how many beds are inside a room. So far the theory. I added a picture of my table "betten_zimmer" which is german and stands for "bed_rooms".

enter image description here I want to make a query which tells me how many beds a room is holding. I want to see every row with the number of beds. My next idea is to add a trigger which will fire when a room holds more than 4 beds or less than 0.

How do I make this query? AND how would the trigger look?

This is my code so far. I have 60 rooms and cant add 60 times OR .. = 5 .. 60

SELECT zimmerid, count(*)
FROM betten_zimmer
WHERE zimmerid = 1 OR zimmerid = 2 OR zimmerid = 3 OR zimmerid = 4
GROUP BY zimmerid

Thanks in advance. If anything is unclear comment this post.

Upvotes: 0

Views: 448

Answers (1)

aleroot
aleroot

Reputation: 72636

If I well understood you only need a BETWEEN construct :

WHERE zimmerid BETWEEN 1 AND 60 

Upvotes: 3

Related Questions