Thread: Insert Loop?

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3

    Insert Loop?


    I have 'questions' that get answered by 'customers'..

    Now when a new question is made ALL CUSTOMERS should have the answer 0 set in the 'answers' table

    So after the INSERT statement for the question i need to loop all the customers and give them 0..

    Is there an easy way to do this?
    Thanks! I'm really confused by this one
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,450
    Rep Power
    1751
    Unless you provide more information about tables and example data the only answer you're likely to get is: UPDATE answers SET answer=0 WHERE something=value;
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    90
    Rep Power
    3
    since the original post is kind of vague, 3 possible scenarios here:

    1. there's one table for customers which has a column for each question.

    -- In this case you want to insert a new column for the new question with the default value set to 0. This would require an ALTER query.

    2. there's one table for questions. with a different column for each customer.

    -- In this case you want to insert a new row for the new question with the default value set to 0. This would require an INSERT query, with the value for each customer column set to 0.

    Neither of these seem likely.

    3. There a table for customers, a table for questions, and a 3rd table for customer answers. the third table has a one to one mapping as in

    answers:
    | key | customer_id | question_id | answer |


    -- Since this is a new question (let's say question 17), you want to INSERT a new row for the new question_id for each customer_id. I assume this is the scenario at hand. For that, you probably want something like this:
    INSERT INTO answers (customer_id, question_id, answer) SELECT DISTINCT customers.id, 17, 0 from customers;

IMN logo majestic logo threadwatch logo seochat tools logo