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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:10:50+00:00 2026-05-13T22:10:50+00:00

I have two tables in a MySql DB: items id (char) name (varchar) description

  • 0

I have two tables in a MySql DB:

items

  • id (char)
  • name (varchar)
  • description (varchar)
  • modified_at (datetime)

folders

  • id (int)
  • name (varchar)
  • parent_id (int)

From a query against a search index I get back an ordered list of ids for items and folders that matched the query. For example, (“ph76gjd”, 34, “rh75ghd”, “gr45gfd”). Of course, in most cases the set will be much larger than this. I also have a list columns that I want in the result set and some of these columns may not exist in both tables. For example, (“id”, “name”, “modified_at”).

I am looking for a way to efficiently build a result set by querying the DB.

  • Do I need to make two separate
    queries since I want data from two
    different tables? If so, then I will
    need to put the results back in order
    somehow?
  • Is there a way to do this with a
    single query using a union or a join?

I am using Ruby on Rails but I don’t think the programming language is really a factor here since I hope to do this mostly in SQL. Any help is appreciated.

Update

Thanks for the answers but I guess I wasn’t clear enough. The tables are unrelated as far as this problem is concerned. I have an array of ids of the rows I want in the order they should appear and all I want is to pull the additional column data from the DB.

  • 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-13T22:10:50+00:00Added an answer on May 13, 2026 at 10:10 pm

    You can get the result you want using the combination of UNION, FIELD and select_rows.

    #ids is an array of id's
    results = Items.connection.select_rows("
      SELECT A.id, A.name, A.description, A.modified_at, NULL AS parent_id
      FROM   items A
      WHERE  A.id IN (#{ids.join(',')})
    
      UNION
    
      SELECT B.id, B.name, NULL AS description, NULL AS modified_at, B.parent_id
      FROM   folders B
      WHERE  A.id IN (#{ids.join(',')})
      ORDER  BY FIELD(id, #{ids.join(','))
    ")
    

    The result is ordered by the order of the id’s. This works for MySQL.

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

Sidebar

Related Questions

I basically have two tables: A(id int, name varchar(10), info varchar(10)) B(id int, item
I have two MySQL tables, locations and items: locations `id` `name` `address` `latitude` `longitude`
In MySQL, I have two tables with a 1:n relationship. Table items has products,
I have two mysql tables: Item containing items that one can buy: CREATE TABLE
I have two MySQL tables: One for items and another to log purchases. I'm
I have two mysql tables, one containing details on cars and one containing all
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
I have two mysql tables - a sales table: +----------------+------------------------------+------+-----+---------+-------+ | Field | Type
I have two tables: Stores and Items. The relationship is: Stores 1---* Items In
I have two tables. One is items , and one is tags . Query's

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.