tags:

views:

73

answers:

1

hi,

I have 5 tables

Questions_Answers

AID  |  QID  |  otherStuff  |  Username  |  UserID  |  otherStuff

Users

UserID  |  Username

Node

ID  |  Type  |  UserId 

Questions

ID  |  oldQ_ID 

Answers

ID  |  Question_Link

I have to fill the empty table Questions_Answers given the information in the other tables.

The Question_Answer table is supposed to contain, for each answer (row), the question it belongs to. Also, for each answer the username and userId.

The Node table contains both questions and answers. The Type field has 2 possible values 'question' and 'answer' specifying if that row is indeed a question or an answer. It contains the usedId who wrote the node.

In the Questions and Answers table, the ID fields are the new IDs.(I've imported these nodes in a new system). These are the IDs that should be added to the question_answer table (AID and QID fields, for answers and questions respectively).

In the Questions table, the oldQ_ID should be only used to assign the answers to their questions, and not added to the Questions_Answer table.

The Question_Link in the Answers table links each answer to its question and refers to the old ID of each question (oldQ_ID) (!).

One more thing. I need to insert the fields in Questions_Answers table in the correct fields, skipping the useless ones.

Thanks..

Candidate solution:

INSERT INTO node_comments (cid, nid, name, uid)
  SELECT a.nid, q.nid, u.name, u.uid
  FROM node AS n
  JOIN content_type_answer AS a ON a.nid  = n.nid
  JOIN content_type_forum AS q ON q.field_oldqid_value = a.field_qlink_value
  JOIN users AS u ON u.uid = n.uid
  WHERE n.type = "answer";
+2  A: 

UPDATE:
I've updated the answer to use Node.ID as the join relation for the Questions table. I think this is close to what you are after, and it shouldn't be too tricky to change if it's not quite right:

INSERT INTO Questions_Answers (AID, QID, Username, UserID)
  SELECT a.ID, q.ID, u.Username, u.UserID
  FROM Node AS n
  JOIN Users AS u ON u.UserID = n.UserID
  JOIN Questions AS q ON q.oldQ_ID = n.ID
  JOIN Answers AS a ON a.Question_Link = q.oldQ_ID
  WHERE n.Type = "answer";

Here's what the query does:

  1. For every row in Node where the Type is answer:
    • Compare Node.UserID with Users.UserID to find the matching rows from the Users table.
    • Compare Node.ID with Questions.oldQ_ID to find the matching rows from the Questions table.
    • Compare Questions.oldQ_ID with Answers.Question_Link to find the matching rows from the Answers table.
  2. Select Answers.ID, Questions.ID, Users.Username, Users.UserID from the match in step 1, and insert these into the Questions_Answers table.

If you need to match Node.ID with an answer rather than a question, then you need a column like Answers.oldA_ID in the Answers table. You cannot compare Node.ID with Questions.ID, as Node.ID is the old ID and Questions.ID is the new ID.

Mike
The otherStuff was just to specify I need to insert into specific fields. But it wasn't necessary, my fault. There is not question ID column in Node table. There is only 1 ID column and it is for both questions and answers. In case you are wondering, the ID field in the tables questions and answers has the same value you have in the node table.
Patrick
@Patrick: I've updated the answer.
Mike
@Mike: thanks. So.. you are looking for all questions in the Node table and adding all the related answers to the Questions_Answers table. Right ? I want all answers be added in the table Questions_Answers.
Patrick
One more thing: the question's user is added to the questions_answers table instead of the answer's user. This is an issue, because I need the answerer instead :(
Patrick
@Patrick: I've updated the `WHERE` clause to select answers instead of questions, which I think should solve the problem.
Mike
@Mike: 1) I wanted to tell you before, there is a misunderstanding: the Questions ID (the new id) are the same of Node ID. The old id matches only with the Question_Link values in the answer table. So the fifth line should be "JOIN Questions AS q ON q.ID = n.ID"... 2) If I just change the last line to type = "answer", and keep the fourth line (q.oldQ_ID = n.ID) I get an empty set because I'm filtering for only answers and looking for questions. I guess it is not correct
Patrick
I think I've solved. I've wrote in my question the query... i will test for a while now...
Patrick