我想结合两个表(Student1,Student2)。
1 - Student1
| student_code | name |
--------------------------
| 1 | katia |
| 2 | roger |
| 3 | ken |
2 - Student2
| student_code | name |
--------------------------
| 3 | katia |
| 4 | roger |
| 5 | ken |
然后我想得到这样的结果。
结果
|table_name| student_code | name |
-------------------------------------
|Student1 | 1 | katia |
|Student1 | 2 | roger |
|Student1 | 3 | ken |
|Student2 | 3 | katia |
|Student2 | 4 | roger |
|Student2 | 5 | ken |
我想只使用ANSI sql。
答案 0 :(得分:4)
您可以使用
SELECT 'Student1' AS table_name, student_code, name FROM Student1
UNION ALL
SELECT 'Student2' AS table_name, student_code, name FROM Student2
答案 1 :(得分:1)
select 'Student1' AS table_name,student_code,name from student1
union
select 'Student2' AS table_name,student_code,name from student2
我假设您知道UNION
和UNION ALL
之间的区别,union
带来了唯一记录,它与UNION PERFORMED ON SETS
相同,而union all
会带来重复的行同样。
在你的情况下,由于第一列区分行,它甚至会带有重复项。
答案 2 :(得分:0)
Use UNION ALL statement :
SELECT 'Student1' as table_name, student_code, name FROM Student1
UNION ALL
SELECT 'Student2' as table_name, student_code, name FROM Student2