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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:36:55+00:00 2026-06-02T14:36:55+00:00

first question from me and it’s a bit of a complex one, so do

  • 0

first question from me and it’s a bit of a complex one, so do guide me through any etiquette you’re all used to! Right, bit of a complex one here so I shall describe what I want first before going into code.

MySQL database, I have a table of organisations, a table of services, and a table that cross-references organisations with services. I’m building a search function that will retrieve the organisations that match ANY selected services, but will sort them based on how many services they match AND list what those services are.

OK, the pertinent bits of code n whatnot:

Organisations table (organisations):

id:   INT, AI
name: VARCHAR(150)

Services table (services):

id:   INT, AI
name: VARCHAR(150)

Cross-reference table (xref_org_services):

org_id:     INT
service_id: INT

I can get a list of organisations that match any of the services I search for:

SELECT DISTINCT organisations.name AS org
FROM organisations
JOIN xref_org_services ON organisations.id=xref_org_services.org_id
WHERE xref_org_services.services_id IN (x, y, z)
ORDER BY org ASC

where x, y, z would be the ID of the services I’ve selected. Done, not a problem.

Now I want to return how many services each of the organisations match, and what they are. I’ve had a try with

SELECT organisations.name AS org, COUNT(xref_org_services.services_id) AS matched

but that’s giving me the number of rows matched total which isn’t what I want. Am I looking at a nested SELECT somewhere to do the count?

Ultimately, my output needs to go something like this:

Org_1 matched 4 services
    - Serv_1
    - Serv_3
    - Serv_4

Org_2 matched 3 services
    - Serv_1
    - Serv_2
    - Serv_4

Org_3 matched 1 service
    - Serv_2

Am I making any sense at all? Let me throw some “data” into this then to see if I can clarify things a bit.

organisations:

id  |  name
-------------------------
1   |  A1 Train
2   |  Faux LLC
3   |  Shakey Practice

services:

id  |  name
-------------------------
1   |  PTS
2   |  Track safety
3   |  Assessors
4   |  Structural
5   |  Signalling

xref_org_services:

org_id  |  services_id
-------------------------
1   |  1
1   |  2
1   |  4
2   |  1
2   |  4
3   |  5

So, this means:

  • A1 Train offers PTS, Track Safety and Structural
  • Faux LLC offers PTS and Structural
  • Shakey Practice offers Signalling

If I want to retrieve all organisations that offer PTS, Track Safety and Structural I want the output to say something along the lines of:

A1 Train matched
    - PTS
    - Track Safety
    - Structural

Faux LLC matched
    - PTS
    - Structural

I hope this makes sense. Pointers, hints, full answers all welcome. Let me know if you need clarifications or further information.

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-06-02T14:36:56+00:00Added an answer on June 2, 2026 at 2:36 pm

    You want to use GROUP BY:

    SELECT
      organisations.name AS org,
      COUNT(*) AS matched,
      GROUP_CONCAT(services.name SEPARATOR "\n") AS services
    FROM
      organisations
      LEFT JOIN xref_org_services
        ON xref_org_services.org_id = organisations.id
      LEFT JOIN services
        ON services.id = xref_org_services.services_id
    WHERE xref_org_services.services_id IN (x, y, z)
    GROUP BY organisations.id
    ORDER BY org ASC;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question here. Writing some code, i receive this error from
This is a two part question from a WPF animation newbie. First, here is
My first question for customization is at here and with help from Mat .
first question here, so hopefully you'll all go gently on me! I've been reading
first question here. I am trying to learn python by stepping through project euler,
this is my first question, although I've already used so many tips from Stack
Pretty simple question from a first-time Ruby programmer. How do you loop through a
My first question here... I want to use an abstract class A from which
First question from me; I'm currently fixing a graphing service that uses XSLFO to
Programming Question First (from w3 schools) The programming example way below (from W3 schools)

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.