Compare columns

Compare columns

am 19.10.2006 15:32:41 von peppe

Hi guys,
I really need some help.
I have two tables tbl_question with 2 columns question_id , question_name
and
tbl_answers with 3 columns answer_id,answer_name and questionFK.
I want to get values from question_id and compare them with questionFK if
some values are missing in questionFK then I need a insert of missing
values.
values from questionFK have to be always same as question_id
Thanx in advance

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Compare columns

am 20.10.2006 00:45:52 von Chris

Peppe wrote:
> Hi guys,
> I really need some help.
> I have two tables tbl_question with 2 columns question_id , question_name
> and
> tbl_answers with 3 columns answer_id,answer_name and questionFK.
> I want to get values from question_id and compare them with questionFK if
> some values are missing in questionFK then I need a insert of missing
> values.
> values from questionFK have to be always same as question_id

I'm not sure if you can do this all in one step but what you want is an
outer join:

select question_id, question_name from tbl_questions q left outer join
tbl_answers a on q.question_id=a.questionFK where a.questionFK IS NULL;

Once you know which questions are unanswered you can insert the rows you
need.

insert into tbl_answers (questionFK) SELECT question_id from
tbl_questions q left outer join tbl_answers a on
q.question_id=a.questionFK where a.questionFK IS NULL;

I'd check the results of the first query before running the second one.

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php