我的问题涉及使用 SQL 通过使用脚本为多组重复值分配组 ID。我已经手动做了一段时间,并意识到,随着数据库的扩展(几千个元素),这将需要很长时间。
这是我的数据库结构:
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 |
现在,如您所见,一个答案可以有多个问题,因此数据库中的db_answer
列中将有重复项。我希望每个db_answer
人都有一个单数answer_id
,如果答案被多次使用,它会被重复。为了说明,我希望我的数据库看起来像:
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 |
我已经广泛寻找执行此操作的脚本,但没有任何运气。就像说明我一直在尝试做的事情一样,我一直在为每个我想添加 id 的答案组使用 SQL:
UPDATE elements SET answer_id = '1' WHERE db_answer = 'Mars is red because blah'