Skip to content
Advertisement

How to fill a SQL column with data (calculated) from another table

I have a question and don’t know how to approach the problem exactly.

I have two tables as following:

“Reference” in the “Client” table is defined as foreign key.

Is it possible to fill the column “count” in the table “Tour” with an automated formula where it counts how many times the t_id appears in the “Client” table?

Something like: COUNT(c_id) FROM clients WHERE reference = t_id

I have read about to create a view but not sure how to fetch the data correctly.

Thanks for your help, Raphael

UPDATE #1:

The workflow as described with the view works perfectly. I’m trying now to fill the column via a trigger but I’m getting an SQL error with the following code:

Advertisement

Answer

The view you have referred to is indeed the way to go here. The view you need to create needs to join the two tables and perform a count aggregation as follows:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement