Re: Compare columns
| From: | Chris | Date: | Thu, 19 Oct 2006 22:45:52 +0000 |
| Subject: | Re: Compare columns | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-42580@lists.php.net to get a copy of this message | ||
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_idI'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/