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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:50:18+00:00 2026-06-11T17:50:18+00:00

If I need to fetch rows from table A with a matching tag in

  • 0

If I need to fetch rows from table A with a matching tag in table B, I’ll just do a simple JOIN ON/WHERE B.name=X. But MySQL will then only return the row from A and the matching tag from B.

What do I do if I want to retrieve all from table A that has a match in table B, and at the same time retrieve all from table B that are linked to the row in table A.

A:
a.id
1
2
3

B: 
b.id   | b.a_id   | b.name
1      | 1        | foo
2      | 1        | bar
3      | 1        | derp
4      | 2        | foo
5      | 2        | derp
6      | 3        | bar

A search for "foo" should then return
a.id   | b.id     | b.a_id   | b.name
1      | 1        | 1        | foo
1      | 2        | 1        | bar
1      | 3        | 1        | derp
2      | 4        | 2        | foo
2      | 5        | 2        | derp

Appreciate any help I can get. Thanks!

  • 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-11T17:50:19+00:00Added an answer on June 11, 2026 at 5:50 pm
    SELECT a.id, b.id, b.name FROM a 
    RIGHT JOIN b ON (a.id = b.a_id) 
    WHERE a.id IN (SELECT b.a_id FROM b WHERE b.name = 'foo')
    

    I think this is what you are looking for. Might not be the most performant solution, but I hope it will work.

    Edit:

    SELECT a_new.id, b.id, b.name FROM 
        (SELECT b.a_id FROM b WHERE b.name = 'foo') AS a_new
    RIGHT JOIN b ON (a_new.id = b.a_id)
    

    I don’t think it’ll get any better than this, basically you don’t need to query the table a anymore; you fetch all rows from b that match ‘foo’, then join those with all relating items.

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

Sidebar

Related Questions

I need to fetch a repeatable random set of rows from a table using
So this is driving me nuts. I need to pull my table rows from
I'm confused when trying to fetch table rows in mysql using C++ with MySQL
I need to fetch data from normalized MSSQL db and feed them in Solr
Lets say I need to fetch some records from the database, and filter them
I often need to fetch tgz files, decompress them, and then delete the tgz.
Im using vb.net and I need to fetch data from two different tables and
Usually, I need to retrieve data from a table in some range; for example,
how can i query my mysql database and fetch rows which are posted in
I need to fetch a number of rows between two column values, using a

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.