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.