I am having a classic case of one-to-many
relation.
Currently I'm having an SQL like given below with no relation whatsoever defined between two tables (my_table
and comments
table):
SELECT
(SELECT
COUNT(1)
FROM
comments
WHERE
comments.gl_account = my_table.gl_account
AND comments.document_number = my_table.document_number
AND comments.division = my_table.division) AS comments_count,
my_table.division,
my_table.gl_account,
my_table.document_number,
my_table.reviewer_group,
my_table.id
FROM
my_table;
It is returning comments
count in addition to other columns from my_table
.
I want to correct this SQL by using 1-to-many
relationship; that from my_table
(one) to comments
table (many).
How do I do this? Note that there are three columns used by inner query above.
Sample SQL snippet hint appreciated, thank you!