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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:44:38+00:00 2026-05-25T01:44:38+00:00

first, you should know that i suck big time at SQL. Here is my

  • 0

first, you should know that i suck big time at SQL.

Here is my problem:

I have 4 tables that i need to be joinned (for usage in SphinxSearch), here is the rough structure:

Accounts

Id Name Category
----------------
1  Test 1
2  Foo  2
3  Bar  1

Category

Id Name
-------
1  Restaurants
2  Store

Accounts_has_subcategory

account_id subcat_id
--------------------
1          1
1          3
2          2

Subcategory

Id Name
-------
1  Chinese
2  Sportswear
3  Delivery

What i want is a resultset looking like this:

accounts.id | accounts.name | category_name | subcategories
-----------------------------------------------------------
1             Test            Restaurants     Chinese, Delivery
2             Foo             Store           Sportswear

Right my query looks like this:

SELECT a.id, a.name, c.name as category, group_concat(subcat.name) as subcategories
FROM accounts AS a
JOIN (account_has_subcategory AS ahs, subcategory AS subcat)
ON (a.id = ahs.account_id AND ahs.subcat_id = subcat.id),
accounts AS a2
JOIN category AS c
ON a2.category = c.id

Like said before, i suck at SQL (as soon as it involves multiple joins or stuff like that basically…). If someone could point me in the right direction or offer a solution (with basic explanation, so i can try and get that in my brain -_-), that would make my day since i’ve been fighting that query for a good 5h now…

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-25T01:44:38+00:00Added an answer on May 25, 2026 at 1:44 am

    Try this?

    SELECT a.id, a.name, 
           c.name as category, 
           group_concat(subcat.name) as subcategories
    FROM accounts AS a
    INNER JOIN account_has_subcategory AS ahs ON a.id = ahs.account_id
    INNER JOIN subcategory AS subcat ON subcat.id = ahs.subcat_id
    INNER JOIN category AS c ON a.category = c.id
    GROUP BY a.id, a.name, c.name
    ORDER BY a.id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First I have to let you know that i am a newbie in this
First of all, I know that I should not use an SQLite database to
I know that this is not the first time a question about this issue
First thing you should know is that this worked on Wednesday of last week
Example: I have two shared objects (same should apply to .dlls). The first shared
I know that the first route will catch most of the paths. However, this
First off: I know that AES is a block cipher and not a hashing
Just a note : I know that in Java finally block should never throw
I'm new to Java so I have a simple question that I don't know
I had a requirement where in the text field the first character should be

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.