I have three tables in a MySQL database:
- stores (PK stores_id)
- states (PK states_id)
- join_stores_states (PK join_id, FK stores_id, FK states_id)
The "stores" table has a single row for every business. The join_stores_states table links an individual business to each state it's in. So, some businesses have stores in 3 states, so they 3 rows in join_stores_states, and others have stores in 1 state, so they have just 1 row in join_stores_states.
I'm trying to figure out how to write a query that will list each business in one row, but still show all the states it's in.
Here's what I have so far, which is obviously giving me every row out of join_stores_states:
SELECT states.*, stores.*, join_stores_states.*
FROM join_stores_states
JOIN stores
ON join_stores_states.stores_id=stores.stores_id
JOIN states
ON join_stores_states.states_id=states.states_id
Loosely, this is what it's giving me:
- store 1 | alabama
- store 1 | florida
- store 1 | kansas
- store 2 | montana
- store 3 | georgia
- store 3 | vermont
This is more of what I want to see:
- store 1 | alabama, florida, kansas
- store 2 | montana
- store 3 | georgia, vermont
Suggestions as to which query methods to try would be just as appreciated as a working query.