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

  • Home
  • SEARCH
  • 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 6802013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:10:17+00:00 2026-05-26T19:10:17+00:00

my situation: Table _customers ———————————————- customer_id | name | active ———————————————- 1 ‘customer I’

  • 0

my situation:

Table “_customers”

----------------------------------------------
customer_id    |    name    |    active
----------------------------------------------
     1          'customer I'       1
----------------------------------------------
     2          'customer II'      0
----------------------------------------------

Table “_projects”

----------------------------------------------
project_id    |    project_name    | customer_id
----------------------------------------------
     1             'project I'           1
---------------------------------------------- 
     2             'project II'          2
----------------------------------------------

many-to-many Table “_project_sections”

----------------------------------------------
section_id  |  project_id
----------------------------------------------
     2             1
----------------------------------------------
     3             1
----------------------------------------------
     4             1
----------------------------------------------
     1             2
----------------------------------------------

In my Case, i need now all Customers, which are ‘active’. Also they must be from a specific Section. So, as a example, i want all active customers from the Section “1”. I also figured it out to get the right data, but – and thats the weird thing – only if need sections with the id > 1. If i try to get all active customers in section 1, the Query still show me all Projects with Section_id 2,3,4…

Query

SELECT c.customer_id, c.name, ps.section_id
FROM _customers c
  INNER JOIN _projects p
     ON p.customer_id = c.customer_id 
  INNER JOIN _project_sections ps
    ON ps.project_id = p.project_id 
  WHERE c.active = 1 AND ps.section_id = 1
GROUP BY c.name
ORDER BY LOWER(c.name)

And the wrong result:

Array
(
[customer_id] => 1
[name] => customer I
[section_id] => 1
)

Maybe someone can help me, because i dont’t get it. Thank you so much!

  • 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-26T19:10:17+00:00Added an answer on May 26, 2026 at 7:10 pm

    You don’t need a GROUP BY clause to write this query.

    First, let’s look at active customers and their projects.

    -- All active customers 
    SELECT c.customer_id, c.name
    FROM customers c
    WHERE c.active = 1;
    
    customer_id    name
    --
    1              customer I
    
    -- All active customers and their projects
    SELECT c.customer_id, c.name, p.project_id, p.project_name
    FROM customers c
    INNER JOIN projects p ON (p.customer_id = c.customer_id)
    WHERE c.active = 1;
    
    customer_id  name          project_id  project_name  
    --
    1            customer I    1           project I
    1            customer I    2           project II
    

    Just one active customer, who has two projects.

    Now let’s look at section 1 projects.

    -- All projects from section 1
    SELECT ps.project_id, ps.section_id
    FROM project_sections ps
    WHERE ps.section_id = 1;
    
    project_id    section_id
    --
    2             1
    
    -- All projects from section 1, including project name
    SELECT ps.project_id, p.project_name, ps.section_id
    FROM project_sections ps
    INNER JOIN projects p ON (p.project_id = ps.project_id)
    WHERE ps.section_id = 1;
    
    project_id    project_name    section_id
    --
    2             project II      1
    

    Just one section 1 project. Now put the pieces together.

    -- All active customers and their projects from section 1
    SELECT c.customer_id, c.name, p.project_id, p.project_name, ps.section_id
    FROM customers c
    INNER JOIN projects p ON (p.customer_id = c.customer_id)
    INNER JOIN project_sections ps ON (p.project_id = ps.project_id)
    WHERE c.active = 1 AND ps.section_id = 1;
    
    customer_id  name          project_id    project_name    section_id
    -- 
    1            customer I    2             project II      1
    

    That’s what I’d expect.

    Later . . .

    I see that you’ve changed the data. If I make the same changes here, then my last query above returns no rows. I think that’s what you said you expected. (And, with your changes to the data, that’s what I’d expect, too.)

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

Sidebar

Related Questions

I have following situation. A main table and many other tables linked together with
I have the following situation: table User (id, name) table Belonging (id, name, color)
The situation is this: a table that is used in a query (name it
I have a customer table in the db that stores the name, address etc,
I have situation where table is created dynamically,i don't know the table name it
Situation: Table TBL has ~10k entries for deletion, Table TBL has 14 child tables
Situation: A table with lots of photos, classified and with votes. I'm currently doing
I've got a situation like this: Table: FunTable ItemKey ItemName ItemPriceEffectiveDate ItemPrice 11 ItemA
Situation: A PHP application with multiple installable modules creates a new table in database
The situation: I fetch a complete table from my sqllite core data database and

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.