Re: Query Help

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Hunter Hillegas <lists(at)lastonepicked(dot)com>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Query Help
Date: 2002-07-24 00:28:25
Message-ID: 20020723170210.X29741-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Tue, 23 Jul 2002, Hunter Hillegas wrote:

> I'm looking for a little query help, if anyone has a free moment...
>
> I have two tables:
>
> CREATE TABLE message_board_topics (
> rec_num integer NOT NULL PRIMARY KEY,
> topic_name varchar(255),
> topic_body text,
> topic_author varchar(20),
> topic_author_email varchar(50),
> topic_date date,
> topic_updated datetime,
> number_of_comments integer
> );
>
> CREATE TABLE message_board_comments (
> rec_num integer NOT NULL PRIMARY KEY,
> topic_id integer REFERENCES message_board_topics,
> comment_parent integer,
> comment_name varchar(255),
> comment_body text,
> comment_author varchar(20),
> comment_author_email varchar(50),
> comment_date date
> );
>
> Anyway, I want to do a quick and dirty search against them... Basically I
> want to find any message_board_topic where the submitted text matches either
> the topic_name, topic_body, topic_author, or the comment_author of any
> related comment...
>
> I tried this:
>
> SELECT * FROM message_board_topics, message_board_comments WHERE
> upper(topic_name) LIKE upper('%test%') OR upper(topic_body) LIKE
> upper('%test%') OR upper(topic_author) LIKE upper('%test%') OR
> (upper(message_board_comments.comment_author) LIKE upper('%test%') AND
> message_board_comments.topic_id = message_board_topics.rec_num) ORDER BY
> message_board_topics.rec_num DESC;
>
> Now, this returns no rows but this query that doesn't search the comments
> table returns a row:
>
> SELECT * FROM message_board_topics WHERE upper(topic_name) LIKE
> upper('%test%') OR upper(topic_body) LIKE upper('%test%') OR
> upper(topic_author) LIKE upper('%test%') ORDER BY
> message_board_topics.rec_num DESC;
>
> Any ideas what I am doing wrong? It would be a big help.

Presumably there's no comment for the matching row. You're doing
an inner join in the first query so it's only going to give you
a row out if there exists a comment whose topic_id is equal to
the other table's rec_num.

You might want to do an outer join instead (something like:
select * from message_board_topics left join message_board_comments
on (message_board_comments.topic_id=message_board_topics.rec_num)
where
upper(topic_name) LIKE upper('%test%') OR upper(topic_body) LIKE
upper('%test%') OR upper(topic_author) LIKE upper('%test%') OR
(upper(message_board_comments.comment_author) LIKE upper('%test%')
order by message_board_topics.rec_num desc;

In response to

  • Query Help at 2002-07-23 23:39:04 from Hunter Hillegas

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2002-07-24 01:54:12 Re: postgresql security mailing list ...
Previous Message Curt Sampson 2002-07-24 00:15:43 Re: Linux max on shared buffers?