我希望能够为每个学生Id在Student_Interest表中插入五个兴趣Id。
我试过了,但我没有得到30行,而是有72行(学生表中有6名学生,兴趣表中有12名学生):
INSERT INTO Student_Interest (Id_Student, Id_Interest, Score)
SELECT
Id_Student
,Id_Interest
,ABS(Checksum(NewID()) % 5) + 1
FROM Student, Interest
ORDER BY NEWID()
谢谢你的帮助。
发布于 2018-06-25 17:53:47
您需要的是CROSS JOIN
:
INSERT INTO Student_Interest (Id_Student, Id_Interest, Score)
SELECT Id_Student, Id_Interest, i.Score
FROM Student s CROSS JOIN
(SELECT TOP (5) Score FROM Interest ORDER BY NEWID()) i;
https://stackoverflow.com/questions/51029090
复制相似问题