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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:49:11+00:00 2026-06-04T16:49:11+00:00

i have table company data is : id | name | email 1 |

  • 0

i have table company data is :

id | name | email
 1 | abc  | any@any.com
 2 | xyz  | as@as.com

other table of category data is :

id | name
1  | cat1
2  | cat2
3  | cat3

and joined table of both category and company is join_cat_company

category_id | company_id | main
          1 |           1| 1
          2 |           1| 0
          3 |           1| 0
          2 |           2| 1
          1 |           2| 0
          3 |           2| 0

in joined table main is use for main category

i want result like :

  company_name | category
           abc | cat1,cat2,cat3
           xyz | cat2,cat1,cat3

the main category must be fist of the category list

  • 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-04T16:49:13+00:00Added an answer on June 4, 2026 at 4:49 pm
    SELECT   company.name AS company_name,
             CONCAT_WS(',',
               GROUP_CONCAT(  maincat.name),
               GROUP_CONCAT(othercats.name)
             ) AS category
    FROM     join_cat_company
           JOIN company                ON join_cat_company.company_id  =   company.id
      LEFT JOIN category AS maincat    ON join_cat_company.category_id =   maincat.id
                                      AND join_cat_company.main = 1
      LEFT JOIN category AS othercats  ON join_cat_company.category_id = othercats.id
                                      AND join_cat_company.main = 0
    GROUP BY company.name
    

    See it on sqlfiddle.


    EDIT

    Further to @lanzz’s excellent observation above, you could simply do:

    SELECT   company.name AS company_name,
             GROUP_CONCAT(
               category.name ORDER BY join_cat_company.main DESC
             ) AS category
    FROM     join_cat_company
           JOIN company  ON  company.id = join_cat_company.company_id
      LEFT JOIN category ON category.id = join_cat_company.category_id
    GROUP BY company.name
    

    See it on sqlfiddle.

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

Sidebar

Related Questions

I have a table that contains company names in this format: Name Name ,
Technology: Hibernate 3.0 Suppose i have Entity class Company @Entity @Table(name=tbl_companies) public class Company
We have a SQL Server table containing Company Name, Address, and Contact name (among
I have very simple data model like this: create table Company ( id int
I have a table, Orgs, that contains the following data: ID Name AddressID 0
I have a table articles that has a column company which has list of
I have a table which contains a list of products for a company. They
I have the following MySQL table structure: num field company phone website 1 Gas
I have two tables, one Company and one Employee: class Company(models.Model): name = models.CharField(max_length=60)
I have a table companies , which has two columns named name and address

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.