mysql。如何在光标中组合几列?

发布于 2025-01-28 18:09:33 字数 1545 浏览 4 评论 0原文

我将非常感谢任何帮助。我的情况.. 我有一张带学生的桌子(我只为此问题编写主要专栏):

| Id | Lastname | Firstname |
_____________________________
| 1  | Smith    | John      |
| 2  | Williams | Robert    |
| 3  | Wilson   | David     |
...

一张带有作品的表:

| Id  | Name    |
_________________
| 1   | Work1   |
| 2   | Work2   |
... 

写这项工作的桌子(可以做同样工作的学生数量可以是任何工作)

| Id | WorkId | StudentId |
___________________________
| 1  | 1      | 1         |
| 2  | 1      | 2         |
| 3  | 2      | 1         |
| 4  | 2      | 2         |
| 5  | 2      | 3         |
...

,我需要做一个过程中的表格(使用光标)这样:

| WorkName     | Students                           |
_____________________________________________________
| Work1        | (Smith J., Williams R.)            |
| Work2        | (Smith J., Williams R., Wilson D.) |
...

在这种情况下,我不完全理解如何使用光标。我的代码:

DELIMITER //
CREATE PROCEDURE Report()
BEGIN    
DECLARE students VARCHAR(200);  
DECLARE id_s INT;
  
DECLARE done INT DEFAULT FALSE;

DECLARE get_cur CURSOR FOR 
SELECT Id, CONCAT(Lastname, ' ', LEFT(Firstname,1), '. ') FROM students;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

open get_cur;
read_loop: LOOP

FETCH get_cur INTO id_s, students;  

IF done THEN
LEAVE read_loop;
END IF;

SELECT w.Name, students
 FROM Work w
JOIN WorkStudent ws ON w.Id=ws.WorkId
JOIN Students s ON s.Id = ws.StudentId 
WHERE s.Id=id_s;

END LOOP;

CLOSE get_cur;  

END //
DELIMITER ;

I will be very grateful for any help. My situation..
I have a table with students (I write only main columns for this question):

| Id | Lastname | Firstname |
_____________________________
| 1  | Smith    | John      |
| 2  | Williams | Robert    |
| 3  | Wilson   | David     |
...

A table with works:

| Id  | Name    |
_________________
| 1   | Work1   |
| 2   | Work2   |
... 

A table where write who did this work (number of students who can do the same work can be any)

| Id | WorkId | StudentId |
___________________________
| 1  | 1      | 1         |
| 2  | 1      | 2         |
| 3  | 2      | 1         |
| 4  | 2      | 2         |
| 5  | 2      | 3         |
...

And I need to make a table in procedure (with using cursor) like this:

| WorkName     | Students                           |
_____________________________________________________
| Work1        | (Smith J., Williams R.)            |
| Work2        | (Smith J., Williams R., Wilson D.) |
...

I don't understand completely how to use cursor in this case. My code:

DELIMITER //
CREATE PROCEDURE Report()
BEGIN    
DECLARE students VARCHAR(200);  
DECLARE id_s INT;
  
DECLARE done INT DEFAULT FALSE;

DECLARE get_cur CURSOR FOR 
SELECT Id, CONCAT(Lastname, ' ', LEFT(Firstname,1), '. ') FROM students;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

open get_cur;
read_loop: LOOP

FETCH get_cur INTO id_s, students;  

IF done THEN
LEAVE read_loop;
END IF;

SELECT w.Name, students
 FROM Work w
JOIN WorkStudent ws ON w.Id=ws.WorkId
JOIN Students s ON s.Id = ws.StudentId 
WHERE s.Id=id_s;

END LOOP;

CLOSE get_cur;  

END //
DELIMITER ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2025-02-04 18:09:33

我解决了问题。对于此决定,我创建了一个新表,如果现在的学生尚未在此表中,则在此表中插入新行,并在表格中使用该表(设置新的学生和逗号)(设置新的学生和逗号)。我是通过光标做到的。
这是最终的正确决定。现在一切都起作用了。

I resolved my problem. For this decision I create a new table and I insert new row in this table if the student is not yet now in this table, and update this table (set new student and comma) if the student is present in table. And I did this with via cursors.
This is the final right decision. Now everything works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文