Well, MySQL doesn’t actually have anything called Foreign Key, it’s just my way of saying a alien column from another table.
Let’s say 2 tables are related, 1 is books and the other is subjects. Any book in book table falls into one of the subjects, thus having a subject column referencing the ID of one of the subjects in subject table.
You need to know the number of books from each of the subjects, namely, 243 books in subject Education, 77 books in Science, etc.
Now how do we do this? Simple. Just perform the following SQL query:
SELECT subject.id, COUNT(book.id) FROM subject, book GROUP BY book.subject HAVING book.subject = subject.id
You should also read:
- MySQL: Find Duplicate Entry / Rows / Records
- MySQL: Add Statistics Column for the Number Count of Records from Another Table
- MySQL, PHP: Display MySQL table fields and data
- MySQL: id BETWEEN start AND end Instead of LIMIT start, step For Better Database Performance
- MySQL: Deleting Duplicate Rows or Records | Selecting Unique Rows into A New Table


Facebook
Twitter
Google Plus
{ 1 trackback }