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 7088293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:46:57+00:00 2026-05-28T07:46:57+00:00

This is the data structure: stores { id, name, city_id, owner_id } cities {

  • 0

This is the data structure:

stores          { id, name, city_id, owner_id }
cities          { id, name }
store_managers  { user_id, store_id }
products        { id, name, store_id }
users           { id, name }
_______________________________________
#note: many users can manage the same store, and the same user can manage 
 many stores.

I need to get, in one query:

  1. The stores that owned by the user that its id I will provide. [from stores, when owner_id = some_number]
  2. For each store, the city that is attached too. [from cities]
  3. The users that have manage privileges to the store. [from store_managers]
  4. the COUNT of products that is assigned to each store.

I need it to be in one query.
The DB is MYSQL


fixed, added the missing foreign key
my mistake, thanks

  • 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-28T07:46:58+00:00Added an answer on May 28, 2026 at 7:46 am

    Assuming that each store is only attached to one city and you forgot the foreign key, it might look something like this (MySQL specific as I learned before):

    SELECT s.id, GROUP_CONCAT(u.name SEPARATOR ', ') as users,
    COUNT(p.*) AS product_count
    FROM stores s
    JOIN city c ON s.city_id = c.id
    JOIN store_managers sm ON sm.store_id = s.id
    JOIN users u ON u.id = sm.user_id
    JOIN products p ON p.store_id = s.id
    WHERE s.owner_id = ?
    GROUP BY s.id
    

    However, you already see that it’s not good fetching all things together, as you can only retrieve the names within one string or you will get several rows containing the same store_id. Unless you really want the names in a concatenated list, you have to explode the again which will lead to problems as soon as a name contains a comma. If you do not group, you have to iterate over all rows with PHP later and manipulate them. Sometimes it’s better to make two queries, but that depends on how you need the names of the users.

    Often when putting to much stuff into one single query you will suffer from problems later. In this case everything is still manageable, but as soon as you want mulitple counts at once it can get dangerous.

    Can still contain some mistakes, just written directly without testing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is about a data structure I thought of. It is a dynamic
I have a data structure that represents C# code like this: class Namespace: string
I got this formula from a data structure book in the bubble sort algorithm.
Here is my data structure alt text http://luvboy.co.cc/images/db.JPG when i try this sql select
The end goal is to have some form of a data structure that stores
Say you have a very simple data structure: (personId, name) ...and you want to
I would like to parse a data structure of closures (in this case a
I have a such a data structure, ID NAME BIRTH AGE SEX ================================= 1
Let's say I have data structures that're something like this: Public Class AttendenceRecord Public
I was going through some data structures and I noticed this as a time

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.