table joins, pivots, and complex attribute queries in mySQL