使用另一列中的值和序列

时间:2015-11-04 14:26:13

标签: oracle11g sequence

我有一个结构

的表TBL_CLASS
CLASS_NAME | CLASS_TYPE
Maths        PRI      
Math         SEC
English      PRI
English      PAR
Physics      PAR
Biology      BIO

我想要做的是使用值Maths和从0001开始而不是1的序列更新ClassCode列,以便上面的表格成为

CLASS_NAME | CLASS_TYPE | CLASS_CODE
Maths        MATH        MATH0001 
Math         MATH        MATH0002
English      ENG         ENG0001
English      ENG         ENG0002
Physics      PHY         PHY0001
Biology      BIO         BIO0001

是否可以在数据库中执行此操作而不用每列填充MATH0001或ENG0001的第一列或创建临时列?

我可以用序列创建一个临时列,但是我仍然需要在列中填入第一个数字,然后选择MAX

SELECT MAX(substr(TEMP_CODE, -4)) AS LAST4DIGIT FROM TBL_CLASS WHERE CLASS_TYPE= 'MATHS'

然后使用更新语句

更新列
UPDATE TBL_CLASS SET CLASS_CODE = CLASS_TYPE||TEMP_COL

我想要实现的是使用类似

的语句更新ClassCode

UPDATE TBL_CLASS SET CLASS_CODE = CLASS_NAME|| <sequence>没有填写每个类的第一个值(0001)并将序列添加到临时列中。

感谢关于我如何处理这个问题的任何指示。

1 个答案:

答案 0 :(得分:0)

此SQL显示了如何使用列输出进行操作。如果这是您想要的,您可以使用它来更新您的表。如果你需要添加一个新的(例如数学)行,需要找到你可以使用的索引号,例如。 /* The Great Quiz Show Game by Seth A---- */ #include <iostream> #include <string> using namespace std; int Guess; int Winnings; //Define question class class Question { private: string Question_Text; string Answer_1; string Answer_2; string Answer_3; string Answer_4; int Correct_Answer; int Prize_Ammount; public :void setValues(string, string, string, string, string, int, int); void askQuestion(); }; int main() { //declare local variables int High_Score[5]; string High_Score_Name[5]; //initialize local variable High_Score[0] = 25000; High_Score[1] = 12000; High_Score[2] = 7500; High_Score[3] = 4000; High_Score[4] = 2000; High_Score_Name[0] = "Gweneth"; High_Score_Name[1] = "Adam"; High_Score_Name[2] = "Nastasia"; High_Score_Name[3] = "Nicolas"; High_Score_Name[4] = "Dani"; cout << "***********************" << endl; cout << "* *" << endl; cout << "* The Great Quiz Show *" << endl; cout << "* *" << endl; cout << "* by *" << endl; cout << "* *" << endl; cout << "* Seth Alpha *" << endl; cout << "* *" << endl; cout << "***********************" << endl; cout << endl; //instances of questions Question q1; Question q2; q1.setValues("What does cout do?", "Eject a CD", "Send text to the printer", "Print text on the screen", "Play a sound", 3, 2500); q2.setValues("What are the two sections in a class?", "Public and local", "Global and local", "Global and private", "Public and private", 4, 5000); //ask questions q1.askQuestion(); q2.askQuestion(); //print HS list cout << "High Score List" << endl; cout << endl; for (int i = 0; i < 5; i++) { cout << High_Score[i] << "" << High_Score_Name[i] << endl; } } void Question::setValues(string q, string a1, string a2, string a3, string a4, int ca, int pa) { Question_Text = q; Answer_1 = a1; Answer_2 = a2; Answer_3 = a3; Answer_4 = a4; Correct_Answer = ca; Prize_Ammount = pa; } void Question::askQuestion() { cout << endl; cout << "What does cout do?" << endl; cout << "1." << Answer_1 << endl; cout << "2." << Answer_2 << endl; cout << "3." << Answer_3 << endl; cout << "4." << Answer_4 << endl; cout << endl; //ask for guess cout << "What is your answer?" << endl; cin >> Guess; //if correct add Prize_Ammount to Winnings if (Guess == Correct_Answer) { cout << endl; cout << "You are correct!" << endl; Winnings = Winnings + Prize_Ammount ; cout << "You just won $" << Prize_Ammount << "!" << endl; cout << "Total winnings: $" << Winnings << endl; cout << endl; } else { cout << endl; cout << "You are not correct" << endl; cout << "Total winnings: $" << Winnings << endl; cout << endl; } } 仅提取数字,然后删除前零(并添加1)。

REGEXP_SUBST

编辑:我没有意识到SELECT class_name, SUBSTR (UPPER (class_name), 1, CASE WHEN UPPER (class_name) LIKE 'MATH%' THEN 4 ELSE 3 END) class_type, SUBSTR ( UPPER (class_name), 1, CASE WHEN UPPER (class_name) LIKE 'MATH%' THEN 4 ELSE 3 END) || LPAD ( ROW_NUMBER () OVER (PARTITION BY class_type ORDER BY class_type), 4, '0') class_code FROM tbl_class 条款可能会引起误解,所以我删除了它。