MySQL One to Many order by number found in joined table
- by Finbarr
Two tables:
table_a
-------
table_a_id: (primary, int)
table_b
-------
table_a_id: (index, int, from table_a)
table_b_value: (varchar)
A one to many relationship between table_a_id and table_b_value.
Given a query like this:
SELECT DISTINCT(table_a_id) FROM table_a
JOIN table_b ON table_a.table_a_id=table_b.table_a_id
I want to order by the number of occurrences of table_a_id in table_b. I'm not really sure how to write this in MySQL.