Is it possible to "concatenate" results of a query?

Go To StackoverFlow.com

0

I'm implementing a book search function based on authors. I should return a query result that contains all the books written by the queried author. However, it is possible that a query for certain author names returns multiple results (e.g., a query of "Smith, W" might match "Smith, Wesson" and "Smith, Will").

So, my problem is how to "concatenate" all the books written by these different authors. If I don't consider the possibility of multiple authors matching a query, I'd go about something like this (in pseudocode, as my real code is quite messy right now):

  • search author table for the author
  • matching the query
  • get author's authorid
  • search book table for book records with the same authorid

However, with the possibility of multiple authors, I have something like this in mind:

// search author table for authors matching the query

foreach(author_match as am){
  // search book table for book records with authorid=am.authorid
  // Is there such thing as this? :\
  book_results += all the rows returned by previous query
}
return book_results;

I'm doing this in PHP (with CodeIgniter Framework) and MySQL. Is there any function/operator that will allow me to do this? Yes, I've tried +=, even if I wasn't expecting much from it, to an ugly output.

Again, my apologies for the pseudocode. I'll try to clean-up my code and edit ASAP but if an answer comes before that, it'd be just as awesome. Thanks.

2012-04-04 20:05
by skytreader
What is wrong with JOINing books and authors in a query - wildplasser 2012-04-04 20:08
I don't know Codeigniter well, but the query you need looks something like: SELECT author.*, book.* FROM author LEFT JOIN book ON author.authorid = book.authorid WHERE author.authorid = <your query author>Michael Berkowski 2012-04-04 20:10
I admittedly haven't looked into JOINS yet. Will try them and report back later. Thanks - skytreader 2012-04-04 20:13


2

I agree with Michael, it seems you need either an INNER JOIN or a LEFT JOIN.

You say you are using Codeigniter so here is a Codeigniter specific example:

$this->db->select('author.*, 
                   book.*');
$this->db->from('authors');
$this->db->join('books', 'books.authorid = authors.id', 'left');
$this->db->where('authors.id', $the_author_id);

Also see:

2012-04-04 20:22
by James Andres
Figured out how to use joins in my query but thanks for the CodeIgniter-specific example (--, - skytreader 2012-04-06 18:28


1

You can use .= to concatenate. However, more efficiently, you can simply specify multiple WHERE clauses in the SQL, so you'd have the following pseudocode:

search author table for authors matching the query
where = 'with '
foreach(author_match as am){
where += 'authorid=' . am.authorid . ' OR' }
where = substr(where[0,strlen(where)-3)
}
search book table for book records [where clause here]
return book_results
2012-04-04 20:11
by Ina


0

Perhaps you can modify your query to use LEFT JOIN. This way you can get multiple query results in one query.

2012-04-04 20:10
by Scott C Wilson
Ummm, MySQL implements UNIONs as, well, UNIONs. LEFT JOIN is an entirely different concept - Michael Berkowski 2012-04-04 20:11


-1

To do a concatenate in PHP, use

.=
2012-04-04 20:07
by PRNDL Development Studios
The += was part of pseudocode, and .= is a string concatenator, supremely unhelpful for appending query results.. - Michael Berkowski 2012-04-04 20:08
He wants to concatenate strings. Unless, of course you have a better way to store the names of things.

Declare a variable, then open the query result loop and .= the results - PRNDL Development Studios 2012-04-04 20:10

Besides, I figured that .= is exclusively string and += is for, ummm, everything else, numeric data and etc. (apparently not). Yes, even in my real code, I tried +=. I want to concatenate records, which, AFAIK are pointers/cursors, not strings - skytreader 2012-04-04 20:10
Ads