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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:43:30+00:00 2026-06-03T01:43:30+00:00

I’m trying to create a complex SQL query (at least for me) but really

  • 0

I’m trying to create a complex SQL query (at least for me) but really don’t know where to start.

Basically, I have a character object, and each character can be made of several parts. For example:

character table:

id | character
--------------
1  | 你
2  | 是
3  | 有

character_parts table:

id | character_id | part_id
---------------------------
1  | 1            | 4
2  | 1            | 9
3  | 1            | 5
4  | 2            | 2
5  | 2            | 34
6  | 2            | 43
7  | 3            | 21
8  | 3            | 16
9  | 3            | 41
10 | 3            | 43

So from that I know that:

Character 1 is made of parts 4, 9, 5
Character 2 is made of parts 2, 34, 43
Character 3 is made of parts 21, 16, 41, 43

Now what I would like to do is select all the characters that end by the specified parts.

For example, if I select all the characters that end by “16, 41, 43”, I’ll get Character 3. If I select all the characters that end by “43”, I’ll get Character 3 and 4.

I assume I need to build some query with subqueries, but not sure how to start, or if it can be done at all. So far I’m just selecting everything that include the required part IDs and then doing the comparison programmatically but this is too slow being I’m selecting way more than needed.

Any suggestion?

  • 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-03T01:43:31+00:00Added an answer on June 3, 2026 at 1:43 am

    You could try group_concat function:
    http://www.sqlite.org/lang_aggfunc.html

    SELECT group_concat(part_id) FROM character_parts WHERE character_id=1

    The query should return 4,9,5.

    The problem is that the order used by group_concat is arbitrary:

    Sqlite group_concat ordering

    So, assuming you have a field position that defines the order of the parts, we can update the query like this:

    SELECT group_concat(part_id) FROM (SELECT part_id FROM character_parts WHERE character_id=1 ORDER BY position ASC)

    The query will now return the 4,9,5 parts exactly the defined order.

    Now that we have this value, we can search though it like a regular string.
    If we want to find all values ending with a certain string, we could use LIKE operator.

    Finally the query would like like this:

    SELECT character_id, parts_concat FROM (
        SELECT character_id, group_concat(part_id) FROM (
            SELECT character_id, part_id FROM character_parts WHERE ORDER BY position ASC
        ) GROUP BY character_id
    ) parts
    WHERE parts_concat LIKE '%,9,5'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites 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.