Hi

I have two tables that I want to use to build a third. They have a many-to-one relationship for one of the columns. Table A has the "one" side of the many-to-one relationship. Table B has many rows that have foreign keys referencing Table A. I want to write a SQL statement that will return rows that tell how many references to each row in Table B are in Table A. For example:

Table A
1 A
2 B
3 C

Table B
1
1
1
2
2
2
2
3
3


I want the statement to return:
A 3
B 4
C 2

Does this make sense or is more information necessary?

Thany you in advance

How about :

SELECT COUNT (*) DISTINCT FROM TABLE_NAME;

Definetely not sure, but just give it a shot?

I found the solution.

SELECT name, COUNT(fk)
FROM TableA INNER JOIN TableB ON TableA.id = TableB.fk
GROUP BY TableB.fk
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.