此查询的结果是否能够在不相互相乘的情况下加入。
Select StudID, StudName, StudYear from tbl_students where StudYear='Third Year';
Select StudID, StudName, StudYear from tbl_students where StudYear='Second Year';
Select StudID, StudName, StudYear from tbl_students where StudYear='First Year';
或者这些查询的“结果”是否可能与9列连接,引用第三年的前3列,第二年的第3列和第1年的最后3列。 谢谢你的回答。
我想到的结果是这样的
StudentID | StudentName | StudentYear ----------------------------------------- 1 | foo1 | 1 ----------------------------------------- 2 | foo2 | 1 ----------------------------------------- 3 | foo3 | 2 ----------------------------------------- 4 | foo4 | 2 ----------------------------------------- 5 | foo5 | 2 ----------------------------------------- 6 | foo6 | 3 -----------------------------------------
StudentID-1 | StudentName-1 | StudentYear-1 |StudentID-2 | StudentName-2 | StudentYear-2 | StudentID-3 | StudentName-3 | StudentYear-3 ---------------------------------------------------------------------------------------------------------------------------------------------------- 1 | foo1 | 1 | 3 | foo3 | 2 |6 | foo6 | 3 ---------------------------------------------------------------------------------------------------------------------------------------------------- 2 | foo2 | 1 | 4 | foo4 | 2 | | | ---------------------------------------------------------------------------------------------------------------------------------------------------- | | | 5 | foo5 | 2 | | | ----------------------------------------------------------------------------------------------------------------------------------------------------
答案 0 :(得分:1)
确定改变了我使用子查询的答案,并将每个子查询视为具有自己的别名列的表。这不会产生您想要的确切结果,这将在未填充的其他6列中产生空值。这就是说我相信这一点非常接近您将在查询中执行您想要的操作。 / p>
SELECT StudentFirst.*, StudentSecond.*, StudentThird.*
FROM (((tbl_students ts
left join (SELECT StudID AS StudIDFirst, StudName AS StudNameFirst, StudYear AS StudYearFirst FROM tbl_students WHERE StudYear = 'First Year') AS StudentFirst on (ts.StudID = StudentFirst.StudIDFirst))
left join (SELECT StudID AS StudIDSecond, StudName AS StudNameSecond, StudYear AS StudYearSecond FROM tbl_students WHERE StudYear = 'Second Year') AS StudentSecond on (ts.StudID = StudentSecond.StudIDSecond))
left join (SELECT StudID AS StudIDThird, StudName AS StudNameThird, StudYear AS StudYearThird FROM tbl_students WHERE StudYear ='Third Year') AS StudentThird on (ts.StudID = StudentThird.StudIDThird))
以下是查询的结果
我会说我不推荐这种方法。 MySQL在处理子查询方面已经有了很多好处,但5.5之前的任何事情都会有糟糕的表现,一旦你达到一定的数据水平,甚至5.5不会很好。同样使用你的mysql语句来格式化结果也不是一个好主意。如果您需要以某种方式查看结果,我将检索记录集,然后将它们以该格式放在应用程序的前端。我希望这会有所帮助。
答案 1 :(得分:0)
是的,您可以将这些查询一起加入
SELECT * FROM
(Select StudentID, StudentName, StudentYear as Third_StudYear
from Students
where StudentYear=3) y3
CROSS JOIN (Select StudentID, StudentName, StudentYear as Second_StudYear
from Students
where StudentYear=2) y2
CROSS JOIN (Select StudentID, StudentName, StudentYear as First_StudYear
from Students
where StudentYear=1) y1
如果这是你真正想做的事情。
祝你好运。
答案 2 :(得分:0)
为了达到你的要求,你必须有一些id类的东西才能加入,所以使用了行号,否则你会有一对多到多个resutls
在SQL Server中工作。所以在MYSQL中尝试一下。
CREATE TABLE #STUDENT(ID INT, NAME VARCHAR(10), STUDENT_YEAR INT);
INSERT INTO #STUDENT VALUES(1,'FOO1',1);
INSERT INTO #STUDENT VALUES(2,'FOO2',1);
INSERT INTO #STUDENT VALUES(3,'FOO3',2);
INSERT INTO #STUDENT VALUES(4,'FOO4',2);
INSERT INTO #STUDENT VALUES(5,'FOO5',2);
INSERT INTO #STUDENT VALUES(6,'FOO6',3);
SELECT * FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY S1.ID ASC) RN,
S1.ID AS [ID_1], S1.NAME AS [NAME_1],S1.STUDENT_YEAR AS [YEAR_1]
FROM #STUDENT S1
where s1.STUDENT_YEAR=1
) T1
FULL JOIN (
SELECT
ROW_NUMBER() OVER (ORDER BY S2.ID ASC) RN,
S2.ID AS [ID_2], S2.NAME AS [NAME_2],S2.STUDENT_YEAR AS [YEAR_2]
FROM #STUDENT S2
where s2.STUDENT_YEAR=2
) T2 ON T1.RN = T2.RN
FULL JOIN (
SELECT
ROW_NUMBER() OVER (ORDER BY S3.ID ASC) RN,
S3.ID AS [ID_3], S3.NAME AS [NAME_3],S3.STUDENT_YEAR AS [YEAR_3]
FROM #STUDENT S3
where s3.STUDENT_YEAR=3
) T3 ON T1.RN = T3.RN
注意:#Student
表示它是临时表。