Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL视图、联合和分组依据_Mysql_Sql_View_Union - Fatal编程技术网

MySQL视图、联合和分组依据

MySQL视图、联合和分组依据,mysql,sql,view,union,Mysql,Sql,View,Union,我正在尝试编写以下SQL查询: Select t1.tms_id, t1.tms_name, t1.Pts from ( SELECT t.tms_id, t.tms_name, SUM(s.lsc_1stscore) as Pts FROM tb_team as t,tb_league as l, tb_game as g, tb_score as s WHERE (t.tms_id = l.lgs_1stplace) AND (l.fk_lsc_id = s.lsc_id) AND (t.f

我正在尝试编写以下SQL查询:

Select t1.tms_id, t1.tms_name, t1.Pts from (
SELECT t.tms_id, t.tms_name, SUM(s.lsc_1stscore) as Pts FROM tb_team as t,tb_league as l, tb_game as g, tb_score as s
WHERE (t.tms_id = l.lgs_1stplace) AND (l.fk_lsc_id = s.lsc_id) AND (t.fk_gms_id = g.gms_id) AND (g.gms_id = 1)
GROUP BY t.tms_name
) t1
union
Select t2.tms_id, t2.tms_name, t2.Pts from (
SELECT t.tms_id, t.tms_name, SUM(s.lsc_2ndscore) as Pts FROM tb_team as t,tb_league as l, tb_game as g, tb_score as s
WHERE (t.tms_id = l.lgs_2ndplace) AND (l.fk_lsc_id = s.lsc_id) AND (t.fk_gms_id = g.gms_id) AND (g.gms_id = 1)
GROUP BY t.tms_name
) t2
union
Select t3.tms_id, t3.tms_name, t3.Pts from (
SELECT t.tms_id, t.tms_name, SUM(s.lsc_3rdscore) as Pts FROM tb_team as t,tb_league as l, tb_game as g, tb_score as s
WHERE (t.tms_id = l.lgs_3rdplace) AND (l.fk_lsc_id = s.lsc_id) AND (t.fk_gms_id = g.gms_id) AND (g.gms_id = 1)
GROUP BY t.tms_name
) t3
union
Select t4.tms_id, t4.tms_name, t4.Pts from (
SELECT t.tms_id, t.tms_name, SUM(s.lsc_4thscore) as Pts FROM tb_team as t,tb_league as l, tb_game as g, tb_score as s
WHERE (t.tms_id = l.lgs_4thplace) AND (l.fk_lsc_id = s.lsc_id) AND (t.fk_gms_id = g.gms_id) AND (g.gms_id = 1)
GROUP BY t.tms_name
) t4
ORDER BY Pts DESC
我需要在这个查询中添加一个group by,特别是在Pts DESC的最后一个订单之前,但是添加group by tms_id示例显示了相同的结果

一位朋友建议我创建视图,但当我尝试显示此错误时:

1349-视图的SELECT在FROM子句中包含一个子查询

我真的不知道我搜索的子查询是什么,我真的不明白。如何重新组织或修复此查询以使用另一个GROUP BY

Extract.

Select t1.tms_id, t1.tms_name, t1.Pts from (
    SELECT t.tms_id, t.tms_name, SUM(s.lsc_1stscore) as Pts FROM tb_team as t,tb_league as l, tb_game as g, tb_score as s
    WHERE (t.tms_id = l.lgs_1stplace) AND (l.fk_lsc_id = s.lsc_id) AND (t.fk_gms_id = g.gms_id) AND (g.gms_id = 1)
    GROUP BY t.tms_name
) t1
下面是来自上面sql语句的子查询

   SELECT t.tms_id, t.tms_name, SUM(s.lsc_1stscore) as Pts FROM tb_team as t,tb_league as l, tb_game as g, tb_score as s
    WHERE (t.tms_id = l.lgs_1stplace) AND (l.fk_lsc_id = s.lsc_id) AND (t.fk_gms_id = g.gms_id) AND (g.gms_id = 1)
    GROUP BY t.tms_name

要在mysql视图中使用子查询,请将每个子查询生成一个视图,然后使用该视图而不是子查询。

谢谢!你的解释让我明白了一切,现在一切都很顺利!!