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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:51:24+00:00 2026-06-06T16:51:24+00:00

(Bonus internets for anyone who can explain how I can do this in both

  • 0

(Bonus internets for anyone who can explain how I can do this in both SQL and SQLAlchemy.)

I have three tables set up like so:

**Bottom:**
id
name
middle_id

**Middle:**
id
name
top_id

**Top:**
id
name

Each bottom has a middle, and each middle has a top. I want to do a search on a database so that when the user types in their query, it will search Bottom, Middle, and Top. If someone searches for an item that happens to be in Top, it will return all the Bottom.* that are related to Top by going through Middle.

Here’s a query that works on two levels:

SELECT *
FROM   Bottom
WHERE  name LIKE '%query%'

UNION

SELECT Bottom.*
FROM   Middle JOIN Bottom ON Bottom.middle_id = Middle.id
WHERE  Middle.name LIKE '%query%'

How do I extend this so that it can also search Top WHERE Top.name LIKE ‘%query%’, but instead of returning Middle.*, it returns Bottom.*? Here’s some SQL that doesn’t work, but I think explains what I’m trying to achieve:

SELECT *
FROM   Bottom
WHERE  name LIKE '%query%'

UNION

SELECT Bottom.*
FROM   Middle JOIN Bottom ON Bottom.middle_id = Middle.id
WHERE  Middle.name LIKE '%query%'

UNION

SELECT Bottom.*
FROM   Top JOIN Middle ON Middle.top_id = Top.id
WHERE  Top.name LIKE '%query%'

In other words, if I have an entry in Bottom that is related to an entry in Middle, and that entry in Middle is related to an entry in Top, if I search Top for the appropriate name, it will return not the Middle entry that is related to it, but the Bottom entry that is related to the related Middle entry.

Here is an example:

Data:

**Bottom**
id        1
name      Gus Fring
middle_id 1

**Middle**
id        1
name      Jesse Pinkman
top_id    1

**Top**
id        1
name      Walter White

If I search for “Walter White” it will return not the Middle relation (Jesse Pinkman), but rather the Bottom relation’s to the Middle relation’s result (Gus Fring).

This issue has been troubling me for a while now and I’d really appreciate any help. Thanks in advance! 🙂

  • 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-06T16:51:26+00:00Added an answer on June 6, 2026 at 4:51 pm

    Try this

    select
      b.id,
      b.name,
      b.middle_id,
      m.top_id,
      m.name,
      t.name
    from bottom as b
      left join (select  id,`name`, top_id  from Middle) as m  on m.id = b.middle_id
      left join (select  id,`name`from Top) as t   on t.id = m.top_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some sql statements that calculates some numbers. It's possible (with bonus points/penalty
As a bonus how do I set this in a config, so that when
bonus props to whoever can figure out why this isn't working. for some reason,
I have a bare-bones sample project here: http://dl.dropbox.com/u/7834263/ExpandingCells.zip In this project, a UITableView has
Is there anyone using git in such a fashion? I would like to distribute
As a bonus to the project I'm currently working on, the people would like
Is rowversion not a valid data type in Microsoft SQL Server 2008 R2? Bonus
I use git-new-workdir to have two working trees for one git repository. This usually
This is Sun JDK 1.6u21, x64. I have a class for the purpose of
UPDATE : I have found the problem that my DP solution didn't handle bonus

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.