MySQL One to Many order by number found in joined table

Posted by Finbarr on Stack Overflow See other posts from Stack Overflow or by Finbarr
Published on 2010-04-10T23:34:00Z Indexed on 2010/04/10 23:43 UTC
Read the original article Hit count: 151

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php