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

  • Home
  • SEARCH
  • 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 8922111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:45:02+00:00 2026-06-15T06:45:02+00:00

My table schema looks like this: table posts (id integer (primary key), parentid integer,

  • 0

My table schema looks like this:

table posts (id integer (primary key), parentid integer, tags varchar, ...)

For any post which is the first post in a thread, their parentid is NULL and tags equals to a string.
Any other post which is not the first post in a thread, their parentid is id of the first post in that thread and tags is NULL.

I could query all “first posts” of a particular topic by

SELECT * FROM posts WHERE tags LIKE '%topic%';

But how do I query all posts including responses of a particular topic?


assume I have records like this

id   parentid   tags
--------------------
123     null   topic
222     123     null
223     123     null
444     null   topic
555     null   hello

how do I get all ids talking about topic, which are 123, 222, 223, 444?

  • 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-15T06:45:03+00:00Added an answer on June 15, 2026 at 6:45 am
    SELECT * FROM posts 
    WHERE tags LIKE '%topic%'
    UNION ALL
    SELECT * FROM posts 
    WHERE PARENTID IN 
      (SELECT ID FROM POSTS WHERE tags LIKE '%topic%')
    

    or

    SELECT p.*, r.* FROM posts p
    LEFT JOIN posts r ON r.ID = p.ID
    WHERE p.tags LIKE '%topic%'
    

    I think the first is cleaner, because it returns one row for the post and one row for each response. The second is easier to implement as a query, but its results are harder to process and it results in more traffic between the database and your script, which is a waste, especially if you’ve got a separate database server.

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

Sidebar

Related Questions

The basic table schema looks something like this (I'm using MySQL BTW): integer unsigned
I have a legacy SQL schema which looks something like this: CREATE TABLE `User`
I have a schema design that looks like this: Table: User Row Column Family
I have a schema that essentially looks like this: CREATE TABLE `data` ( `id`
I have a table in a Rails application which (in schema.rb) looks like: create_table
I have a table in PostgreSQL where the schema looks like this: CREATE TABLE
My database schema looks like this: Table t1: id valA valB Table t2: id
My nycgrid table with the following schema id,x1,x2,y1,y2 . And it looks like following
At a glance, the database schema looks like this: The schema has to be
My Project Model looks like this: # == Schema Information # Schema version: 20101117094659

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.