I am trying to write a SQL statement between two tables where there is a has-many relation between two tables going one way and returns only the rows with more than one relation. I’m probably phrasing this horribly so here’s the example.
Let’s say table A has a column “key_a” that is a primary key for Table A. Table B has two columns. The first is called “key_b” which is a primary key for Table B and a second column called “key_a” which is a foreign key with a relation to “A.has_a”. Application logic has been implemented to ensure that While a row entry in Table A can have a relation from 0 to n rows in Table B, rows in Table B have a relation to exactly one row in Table A.
How can I write an SQL query that will return row entries in “A.key_a” that have more than one row in Table B mapping a relation to it through the “B.key_a” column?
will give you the data from
table_awhere there are multiple matching rows intable_b. If you are only interested in getting thekey_avalues that have multiple rows intable_brather than getting all the data fromtable_a, you can remove the reference totable_aentirely and just do theGROUP BYandHAVINGontable_b.