mysql错误!是否可以在 mysql 中使用 table 变量?
mysql error! Is it possible to use table variables in mysql?
我正在学习 mysql 并且我正在测试分配 table 变量以从相同的 table 检索信息。我有一个简单的 table 学生,我想要一对具有相同 GPA 的学生(来自同一个 table -student-。所以我为学生 1 (S1) 和另一个分配了一个变量一个用于 S2,我希望成对的不同学生具有相同的 GPA,但我得到了其他东西;另一件事要提到的是,如果我将查询结果导出到 csv 文件,只有 S1 数据在 table、S2信息为空白(空单元格),变量在 mysql 中的工作方式是否不同?
这是 SQL 课程中的查询(我无法在 mysql 中复制)
SELECT s1.sID, s1.sName, s1.GPA, s2.sID, s2.sName, s2.GPA
FROM student s1, student s2
WHERE s1.GPA = s2.GPA AND s1.sID < s2.sID;
这里是 table 和数据:
--tables
CREATE TABLE college(
cName VARCHAR(15),
state VARCHAR(15),
enrollment INT,
PRIMARY KEY(cName)
);
CREATE TABLE student(
sID INT,
sName VARCHAR(40),
GPA DECIMAL(3,1),
sizeHS INT,
PRIMARY KEY(sID)
);
CREATE TABLE application(
sID INT,
cName VARCHAR(20),
major VARCHAR(20),
decision VARCHAR(1),
PRIMARY KEY(sID, cName, major)
);
--college data injection
INSERT INTO college VALUES('UNAM', 'Mexico City', 360000);
INSERT INTO college VALUES('UAM', 'Mexico City', 250000);
INSERT INTO college VALUES('Tec Culiacan', 'Sinaloa', 9000);
INSERT INTO college VALUES('UAS', 'Sinaloa', 110000);
INSERT INTO college VALUES('UACH', 'Chihuahua', 65000);
INSERT INTO college VALUES('Tec Monterrey', 'Nuevo Leon', 7500);
--student data injection
INSERT INTO student VALUES(9832, 'Juan Perez', 85.0, 3000);
INSERT INTO student VALUES(0219, 'Maria Gonzalez', 92.0, 9000);
INSERT INTO student VALUES(1683, 'Pedro Sanchez', 79.0, 13000);
INSERT INTO student VALUES(8329, 'Pilar Madrid', 83.0, 1500);
INSERT INTO student VALUES(0921, 'Eva Quintero', 80.0, 7900);
INSERT INTO student VALUES(9816, 'Francisco Hernandez', 96.0, 6500);
--application data injection
INSERT INTO application VALUES(9816, 'UNAM', 'CS', 'Y');
INSERT INTO application VALUES(9816, 'UAM', 'EE', 'Y');
INSERT INTO application VALUES(8329, 'Tec Monterrey', 'MGMT', 'N');
INSERT INTO application VALUES(8329, 'UNAM', 'Admin', 'N');
INSERT INTO application VALUES(1683, 'UAM', 'Biology', 'N');
INSERT INTO application VALUES(1683, 'UAS', 'Biology', 'Y');
INSERT INTO application VALUES(0219, 'Tec Culiacan', 'CS', 'Y');
INSERT INTO application VALUES(0219, 'UACH', 'CS', 'Y');
INSERT INTO application VALUES(0921, 'UAS', 'Industrial', 'Y');
INSERT INTO application VALUES(0921, 'Tec Culiacan', 'Mechatronic', 'N');
INSERT INTO application VALUES(9832, 'UAM', 'Arch', 'Y');
INSERT INTO application VALUES(0921, 'UACH', 'Industrial', 'N');
这是查询的预期结果:
您的查询工作正常,但首先确保您的学生在 Student
table 中有相同的 GPA,然后再次 运行 查询。
--student data injection
INSERT INTO student VALUES(9832, 'Juan Perez', 85.0, 3000);
INSERT INTO student VALUES(0219, 'Maria Gonzalez', 92.0, 9000);
INSERT INTO student VALUES(1683, 'Pedro Sanchez', 96.0, 13000);
INSERT INTO student VALUES(8329, 'Pilar Madrid', 92.0, 1500);
INSERT INTO student VALUES(0921, 'Eva Quintero', 80.0, 7900);
INSERT INTO student VALUES(9816, 'Francisco Hernandez', 96.0, 6500);
运行查询的结果:
我正在学习 mysql 并且我正在测试分配 table 变量以从相同的 table 检索信息。我有一个简单的 table 学生,我想要一对具有相同 GPA 的学生(来自同一个 table -student-。所以我为学生 1 (S1) 和另一个分配了一个变量一个用于 S2,我希望成对的不同学生具有相同的 GPA,但我得到了其他东西;另一件事要提到的是,如果我将查询结果导出到 csv 文件,只有 S1 数据在 table、S2信息为空白(空单元格),变量在 mysql 中的工作方式是否不同?
这是 SQL 课程中的查询(我无法在 mysql 中复制)
SELECT s1.sID, s1.sName, s1.GPA, s2.sID, s2.sName, s2.GPA
FROM student s1, student s2
WHERE s1.GPA = s2.GPA AND s1.sID < s2.sID;
这里是 table 和数据:
--tables
CREATE TABLE college(
cName VARCHAR(15),
state VARCHAR(15),
enrollment INT,
PRIMARY KEY(cName)
);
CREATE TABLE student(
sID INT,
sName VARCHAR(40),
GPA DECIMAL(3,1),
sizeHS INT,
PRIMARY KEY(sID)
);
CREATE TABLE application(
sID INT,
cName VARCHAR(20),
major VARCHAR(20),
decision VARCHAR(1),
PRIMARY KEY(sID, cName, major)
);
--college data injection
INSERT INTO college VALUES('UNAM', 'Mexico City', 360000);
INSERT INTO college VALUES('UAM', 'Mexico City', 250000);
INSERT INTO college VALUES('Tec Culiacan', 'Sinaloa', 9000);
INSERT INTO college VALUES('UAS', 'Sinaloa', 110000);
INSERT INTO college VALUES('UACH', 'Chihuahua', 65000);
INSERT INTO college VALUES('Tec Monterrey', 'Nuevo Leon', 7500);
--student data injection
INSERT INTO student VALUES(9832, 'Juan Perez', 85.0, 3000);
INSERT INTO student VALUES(0219, 'Maria Gonzalez', 92.0, 9000);
INSERT INTO student VALUES(1683, 'Pedro Sanchez', 79.0, 13000);
INSERT INTO student VALUES(8329, 'Pilar Madrid', 83.0, 1500);
INSERT INTO student VALUES(0921, 'Eva Quintero', 80.0, 7900);
INSERT INTO student VALUES(9816, 'Francisco Hernandez', 96.0, 6500);
--application data injection
INSERT INTO application VALUES(9816, 'UNAM', 'CS', 'Y');
INSERT INTO application VALUES(9816, 'UAM', 'EE', 'Y');
INSERT INTO application VALUES(8329, 'Tec Monterrey', 'MGMT', 'N');
INSERT INTO application VALUES(8329, 'UNAM', 'Admin', 'N');
INSERT INTO application VALUES(1683, 'UAM', 'Biology', 'N');
INSERT INTO application VALUES(1683, 'UAS', 'Biology', 'Y');
INSERT INTO application VALUES(0219, 'Tec Culiacan', 'CS', 'Y');
INSERT INTO application VALUES(0219, 'UACH', 'CS', 'Y');
INSERT INTO application VALUES(0921, 'UAS', 'Industrial', 'Y');
INSERT INTO application VALUES(0921, 'Tec Culiacan', 'Mechatronic', 'N');
INSERT INTO application VALUES(9832, 'UAM', 'Arch', 'Y');
INSERT INTO application VALUES(0921, 'UACH', 'Industrial', 'N');
这是查询的预期结果:
您的查询工作正常,但首先确保您的学生在 Student
table 中有相同的 GPA,然后再次 运行 查询。
--student data injection
INSERT INTO student VALUES(9832, 'Juan Perez', 85.0, 3000);
INSERT INTO student VALUES(0219, 'Maria Gonzalez', 92.0, 9000);
INSERT INTO student VALUES(1683, 'Pedro Sanchez', 96.0, 13000);
INSERT INTO student VALUES(8329, 'Pilar Madrid', 92.0, 1500);
INSERT INTO student VALUES(0921, 'Eva Quintero', 80.0, 7900);
INSERT INTO student VALUES(9816, 'Francisco Hernandez', 96.0, 6500);
运行查询的结果: