Jeff C. wrote:
On Dec 9, 11:06ï¿½am, Jay M. [email protected] wrote:
@books = Books.find_by_sql [“SELECT * FROM books WHERE title IN SELECT
title FROM authors WHERE name = ?”, params[:author][:name]]
One problem is where to put the () that will enclose the inner select.
Or may be it is not needed.
I don’t understand what tables and columns you’ve got here. Assuming
you have a books table that has a title column, and an authors table
with a name column, how do you relate books to authors? Is there a
foreign key that relates the tables?
Here is the table structure:
Author has id, name, title, book_id and other columns
Book has id, title, isbn, and other columns.
They both have the title column. And, yes, authors has book_id as a
The inner query is to select title where name = the name that was passed
in as the query condition. Then the outer query will select all rows
where title is equal to the title returned by the inner query.
The strange thing here is, title has a unique entry in authors table,
but title has duplicate entries in the book table.
So, the main point here is to get all rows from the book table that has
the same value as the title that was returned from the authors table,
which was selected based on the name that was passed in.
I don’t have a control over the table structure. So, changing it is not