UNION statement to sort search results twice
Jan Eden <[email protected]>
| Newsgroups | gmane.comp.db.mysql.perl |
|---|---|
| Message-ID | <r02010400-1038-A629D5D87C4811D9AF6F000A959B4026@[10.149.23.154]> |
Hi,
I wrote a search script which can search only the page titles and author names for articles on my site or the titles/authors and the pages' content.
To get the results for the two types in two sets, I used a UNION construct:
if ($search_string) {
my $where_author_title = qq{(authors.last_name LIKE '\%$search_string\%' OR authors.first_name LIKE '\%$search_string\%' OR pages.title LIKE '\%$search_string\%')};
my $where_content = qq{MATCH(content) AGAINST ('$search_string' IN BOOLEAN MODE)};
my @wheres = ( $where_author_title );
push @wheres, $where_content if $volltext;
my $query = join 'UNION', map(qq{(SELECT pages.page_id, pages.title, authors.first_name, authors.last_name, pages.visible, pages.user_id FROM pages JOIN authors USING (author_id) WHERE $_ AND site_id = $sitemode ORDER BY pages.title)}, @wheres);
Later on, I test each row returned by the UNION statement if the $search_string is either in the title or in the author's name. If not, I print a dividing line and a subheader once.
The result looks ok, something like:
Search results for 'Perl'
Title/author
...
...
...
Content
...
...
...
Now is there a better/more elegant way to do this? I am sure there is, and I would be grateful for a pointer.
Thanks,
Jan
--
Hanlon's Razor: Never attribute to malice that which can be adequately explained by stupidity.
--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/[email protected]