Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7034619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:12:29+00:00 2026-05-28T01:12:29+00:00

I have three tables in a MySQL database: stores (PK stores_id) states (PK states_id)

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:12:30+00:00Added an answer on May 28, 2026 at 1:12 am

    If you need the list of states as a string, you can use MySQL’s GROUP_CONCAT function (or equivalent, if you are using another SQL dialect), as in the example below. If you want to do any kind of further processing of the states separately, I would prefer you run the query as you did, and then collect the resultset into a more complex structure (hashtable of arrays, as a simplest measure, but more complex OO designs are certainly possible) in the client by iterating over the resulting rows.

    SELECT stores.name,
      GROUP_CONCAT(states.name ORDER BY states.name ASC SEPARATOR ', ') AS state_names
    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
    GROUP BY stores.name
    

    Also, even if you only need the concatenated string and not a data structure, some databases might not have an aggregate concatenation function, in which case you will have to do the client processing anyway. In pseudocode, since you did not specify a language either:

    perform query
    stores = empty hash
    for each row from query results:
      get the store object from the hash by name
      if the name isn't in the hash:
        put an empty store object into the hash under the name
      add the state name to the store object's stores array
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three mysql table from same database Db1. Three tables have following columns.
I have three tables in a Mysql database - countries, cities and hotels. Their
I have three or four tables in a MySQL database associated with an upcoming
Here's some background info. I have three MySQL tables (all InnoDB). The first table
We have a relational database (MySql) with a table that stores Whatever. This table
I have the following three tables in a MySQL database in order to give
I have a MySql database with three tables I need to combine in a
I have one problem... There is a table in my MySQL database that stores
I have a MySQL database set-up in a hierarchy style. There are 4 tables
I have the following task: there are a simple table from mysql database: <html>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.