Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7815423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:31:06+00:00 2026-06-02T05:31:06+00:00

My question involves the use of SQL to assign multiple groups of duplicated values

  • 0

My question involves the use of SQL to assign multiple groups of duplicated values a group id, through the use of a script. I’ve been doing it by hand for a bit and realized that, with the expanse of the database (a couple thousand elements), it would take ages.

Here is my DB structure:

id  | db quesition         | db keywords           | answer id  | db answer                    |
------------------------------------------------------------------------------------------------
 0  | Why is Mars red?     | [why,mars,red]        | 0          | Mars is red because blah     |

 1  | How is Mars red?     | [how,mars,red]        | 0          | Mars is red because blah     |

 2  | What makes Mars red? | [what,makes,mars,red] | 0          | Mars is red because blah     |

 3  | Is Mars very rocky?  | [is,mars,rocky]       | 0          | Yes Mars is rocky blahbla    |

 4  | Does Mars have rocks?| [mars,have,rocks]     | 0          | Yes Mars is rocky blahbla    |

 5  | What is the Sun?     | [what,is,sun]         | 0          | The Sun is our solar blah    |

 6  | What is a star?      | [what,is,star]        | 0          | A star is a ball of hot blah |

Now, as you can see, there can be multiple questions for one answer, therefore the database will have duplicates in the db_answer column. I would like for each db_answer to have a singular answer_id that would be repeated if the answer is used more than once. To illustrate, I’d like for my db to look like:

id  | db quesition         |  db keywords          | answer id | db answer                    |
-----------------------------------------------------------------------------------------------
 0  | Why is Mars red?     | [why,mars,red]        | 1         | Mars is red because blah     |

 1  | How is Mars red?     | [how,mars,red]        | 1         | Mars is red because blah     |

 2  | What makes Mars red? | [what,makes,mars,red] | 1         | Mars is red because blah     |

 3  | Is Mars very rocky?  | [is,mars,rocky]       | 2         | Yes Mars is rocky blahbla    |

 4  | Does Mars have rocks?| [mars,have,rocks]     | 2         | Yes Mars is rocky blahbla    |

 5  | What is the Sun?     | [what,is,sun]         | 3         | The Sun is our solar blah    |

 6  | What is a star?      | [what,is,star]        | 4         | A star is a ball of hot blah |

I have looked extensively for scripts that do this, but haven’t had any luck. Just as a note to show what I’ve been trying to do, I have been using the SQL for each answer group I wanted to add an id to:

UPDATE elements SET answer_id = '1' WHERE db_answer = 'Mars is red because blah' 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:31:07+00:00Added an answer on June 2, 2026 at 5:31 am

    This would be pretty easy with a PHP script:

    $query = mysql_query("SELECT DISTINCT db_answer FROM elements");
    $i = 1;
    while ($row = mysql_fetch_row($query))
    {
        mysql_query("UPDATE elements SET answer_id = {$i} WHERE db_answer = '{$row[0]}'");
        $i++;
    }
    

    However, I think it might be wise to store the answers in a separate table and just keep the answer_id in the elements table. That way you avoid unnecessarily duplicating information.


    EDIT :

    As @mdoyle suggested, I think it would be best to use four tables:

    CREATE TABLE questions (
        questionID INT NOT NULL AUTO_INCREMENT,
        question VARCHAR(128),
        answerID INT,
        PRIMARY KEY (questionID),
        FOREIGN KEY (answerID) REFERENCES answers (answerID)
    );
    
    CREATE TABLE answers (
        answerID INT NOT NULL AUTO_INCREMENT,
        answer VARCHAR(128),
        PRIMARY KEY (answerID)
    );
    
    CREATE TABLE keywords (
        keywordID INT NOT NULL AUTO_INCREMENT,
        keyword VARCHAR(16),
        PRIMARY KEY (keywordID)
    );
    
    CREATE TABLE question_keywords (
        questionID INT,
        keywordID INT,
        FOREIGN KEY (questionID) REFERENCES questions (questionID),
        FOREIGN KEY (keywordID) REFERENCES keywords (keywordID)
    );
    

    The relationship between the answers table and the questions table is one-to-many (one answer may apply to many questions), so you have two tables. This assumes that each question can have one and only one answer. If this isn’t the case, and there is a possibility that one question might have two acceptable answers, then the relationship becomes many-to-many (continue reading for how to set up tables for a many-to-many relationship).

    The relationship between the questions table and the keywords table is many-to-many (many questions may use many keywords), so you have three tables. One holds the questions (one row per question), one holds the keywords (one row per keyword) and the third ties the two together. The question_keywords table will have multiple rows with the same questionID and multiple rows with the same keywordID. So if questionID 5 has three keywords, then there will be three entries in the question_keywords table with a questionID of 5.

    For any one-to-one relationships, you are generally safe just making an additional column in the same table, so you will have one table for that relationship.

    NOTE: Feel free to change the lengths of the VARCHAR columns. I picked values that might be OK, based on your examples, but if the questions and/or answers can be longer, then you may need to increase this size.


    After creating these tables, you can populate them by doing something like this:

    $query = $mysql_query("SELECT * FROM elements") or die(mysql_error());
    echo "About to enter while-loop<br />";
    $i = 1;
    while ($row = mysql_fetch_assoc($query))
    {
        echo "loop ". $i++ ."<br />";
        $answerID = -1;
    
        $querystr = "SELECT answerID FROM answers WHERE answer = '{$row["db_answer"]}'";
        echo "Getting answerID. query: {$querystr}<br />";
        $query = mysql_query($querystr) or die($mysql_error());
        if (!(list($answerID) = mysql_fetch_row($query)))
        {
            $querystr = "INSERT INTO answers (answer) VALUES ('{$row["db_answer"]}')";
            echo "Answer did not exist, inserting now. query: {$querystr}<br />";
            mysql_query($querystr) or die(mysql_error());
            $answerID = mysql_insert_id();
        }
    
        $querystr = "INSERT INTO questions (questionID, question, answerID) VALUES ('{$row["id"]}', '{$row["db_question"]}', '{$answerID}')";
        echo "Inserting question. query: {$querystr}<br />";
        mysql_query($querystr) or die(mysql_error());
    
        $keywords = explode(",", trim($row["db_keywords"], "[]"));
        echo "keywords = ". print_r($keywords, true) ."<br />";
        foreach ($keywords as $keyword)
        {
            $keywordID = -1;
            $querystr = "SELECT keywordID FROM keywords WHERE keyword = '{$keyword}'";
            echo "Getting keywordID. query: {$querystr}<br />";
            $query = mysql_query($querystr) or die(mysql_error());
            if (!(list($keywordID) = mysql_fetch_row($query)))
            {
                $querystr = "INSERT INTO keywords (keyword) VALUES ('{$keyword}')";
                echo "Keyword did not exist, inserting now. query: {$querystr}<br />";
                mysql_query($querystr) or die(mysql_error());
                $keywordID = mysql_insert_id();
            }
    
            $querystr = "INSERT INTO question_keywords (questionID, keywordID) VALUES ('{$row["id"]}', '{$keywordID}')";
            echo "Inserting question keyword. query: {$querystr}<br />";
            mysql_query($querystr) or die(mysql_error());
        }
    }
    

    Once you have done this, and verified that the four tables are populated correctly, you no longer need to use the elements table at all. Just use those four tables (questions, answers, keywords, and question_keywords).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question involves this simple walkthrough shown in the article Preserve Size and Location
This question involves 3 tables and 1 form in my Access database. The tables
I have a question which involves calling a function with 2 parameters of a
I have a question which I think involves conditional entropy in the field of
This is more a general question but my particular case involves a ruby/rails app
This is mostly a data warehouse philosophy question. My project involves an Oracle forms
I'm not sure what to call this question, since it involves a variety of
I am going to work on a project that involves Paypal function. My question
I am writing an asp.net web app which involves the use of the FileUpload
See this related question on more generic use of the Boost Random library. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.