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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:56:44+00:00 2026-05-16T14:56:44+00:00

I have a SQL query: SELECT b . * , CONCAT( GROUP_CONCAT( c.name )

  • 0

I have a SQL query:

SELECT b . * , CONCAT( GROUP_CONCAT( c.name ) ) categories, CONCAT( GROUP_CONCAT( c.id ) ) cids
FROM books b
JOIN categories_of_books cb ON b.id = cb.book_id
JOIN categories c ON c.id = cb.category_id
GROUP BY b.id

And it returns to me in categories alias all categories of books and in cids all categories ids.

And when i adding WHERE clause like this:

WHERE c.id = 10

In categories I`ll have only one category and it is clear. But how can i write SQL query which will return me all categories of books in categories alias with limiting condition:

WHERE c.id = 10

[EDIT 1]

Only variant:

SELECT b . * , GROUP_CONCAT( c.name ) categories, GROUP_CONCAT(CAST(c.id AS CHAR)) cid
FROM books b
JOIN categories_of_books cb ON b.id = cb.book_id
JOIN categories c ON c.id = cb.category_id
WHERE b.id in (SELECT categories_of_books.book_id FROM categories_of_books join categories on categories_of_books.category_id = categories.id WHERE categories.id = 10)
GROUP BY b.id

Do you know other better variants?

  • 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-16T14:56:45+00:00Added an answer on May 16, 2026 at 2:56 pm

    What you’re asking for, which is basically (if I’ve interpreted your requirements correctly):

    • For all the records in the books table, return one row
    • If a book has more than one category assigned, for the column categories to contain “category_1, category_2, category3”, i.e. the concatenation of all category names
    • Only if one of the categories is categories.id = 10

    What you need to do is start by selecting all books that are in the category with id = 10:

    SELECT cb.book_id 
    FROM   categories_of_books cb
    WHERE  cb.category_id = 10
    

    Now you want all books where they’re in that subset:

    SELECT b.*
    FROM   books b
    WHERE  b.id IN
    (
        SELECT cb.book_id 
        FROM   categories_of_books cb
        WHERE  cb.category_id = 10
    )
    

    Now you can transform it back into your complicated grouping query:

    SELECT b.*, 
           CONCAT(GROUP_CONCAT(c.name)) categories, 
           CONCAT(GROUP_CONCAT(c.id)) cids
    FROM   books b
    JOIN   categories_of_books cb 
           ON b.id = cb.book_id
    JOIN   categories c 
           ON c.id = cb.category_id
    WHERE  b.id IN
    (
        SELECT cb.book_id 
        FROM   categories_of_books cb
        WHERE  cb.category_id = 10
    )
    GROUP BY b.id
    

    There’s really no tidier/simpler way that I can think of to achieve this. As an aside, it’s worth taking the time to lay your code out using whitespace as I’ve done above as it’s generally considered easier to read that way, anyone who has to maintain your code in the future will probaly thank you for it =)

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

Sidebar

Related Questions

i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
i have this sql query select * from table where name like ? but
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
Let's say I have the following SQL query SELECT id, name, title, description, time
I have this SQL query select case when AllowanceId is null then 2 else
I have this sql query: SELECT S.SEARCH, S.STATUS, C.TITLE AS CategoryName, E.SEARCH_ENGINES AS Engine,
I have this SQL query: SELECT DISTINCT tb.SERIAL_NUM, tb.REVISION_NUM, tb.JOB_NUM, tb.JOB_TOTAL, tb.TEST_PROC, tb.BOX_NUM, tb.TEST_BENCH,
I have the following SQL query: SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID, ra.BEZEICHNUNG
I have the following SQL query SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID, ra.BEZEICHNUNG
I have a simple SQL query: Select ID, COUNT(ID) as Selections, OptionName, SUM(Units) as

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.