MYSQL中的这种关节操作

时间:2017-07-04 19:50:57

标签: mysql sql

我有3个表,结构几乎与下面的数据一样,

table name = myshop structure (id_no, time, teams, home, draw, away)

table name = myshop2 structure (id_no, time, teams, home, draw, away)

table name = myshop3 structure (id_no, teams, home, draw, away)

 `TABLE NAME: MYSHOP
  ID_NO TIME    TEAMS                          HOME DRAW    AWAY
  209   00:00   FK PARTIZAN vs CSKA MOSCOW  3.3 3.75    1.85
  210   00:00   RAPID VIENNA vs CELTIC          2.6 3.3 2.3
  211   00:00   SPARTAK MOSCOW vs FK VOZDOVAC   1.3 5.2 6.75
  212   00:00   LUDOGORETS vs DINAMO MOSCOW 2.2 3.35    2.6
  213   00:00   FC THUN vs FC ZURICH            1.9 3.8 2.6
  214   01:00   FC NURNBERG vs BOHEMIANS 1905   1.45    3.9 4.6
  215   01:00   LILLESTROM vs STROMSGODSET  1.0 3.5 3.35
  216   01:00   HALMSTAD vs IF ELFSBORG         2.4 3.5 2.0
  217   05:00   FLANDRIA vs CHACARITA JUNIORS   3.78    2.67    2.54
  218   05:00   CA RIVER PLATE vs DANUBIO   3.25    3.15    2.7
  219   05:00   JUVENTUD vs CERRO           3.55    3.15    2.3

  TABLE NAME: MYSHOP 2
  ID_NO TIME    TEAMS                          HOME DRAW    AWAY
  9     00:00   FK PARTIZAN vs CSKA MOSCOW      2.3 3.75    1.85
  10    00:00   RAPID VIENNA vs CELTIC          1.6 3.3      2.3
  11    00:00   SPARTAK MOSCOW vs FK VOZDOVAC   2.3 5.2      6.75
  12    00:00   LUDOGORETS vs DINAMO MOSCOW     1.2 3.35     2.6
  13    00:00   FC THUN vs FC ZURICH            2.9 3.8      2.6
  14    01:00   FC NURNBERG vs BOHEMIANS 1905   2.453.9      4.6
  15    01:00   LILLESTROM vs STROMSGODSET      1.0 3.5      3.35
  16    01:00   HALMSTAD vs IF ELFSBORG         2.4 3.5      2.0
  17    05:00   FLANDRIA vs CHACARITA JUNIORS   1.782.67     2.54
  18    05:00   CA RIVER PLATE vs DANUBIO   1.25    3.15    2.7
  19    05:00   JUVENTUD vs CERRO           1.55    3.15    2.3


  TABLE NAME: MYSHOP 3
  ID_NO TEAMS                          HOME DRAW    AWAY
  33    FK PARTIZAN vs CSKA MOSCOW     2.3  2.75    2.85
  10    RAPID VIENNA vs CELTIC          1.6 2.3 2.3
  11    SPARTAK MOSCOW vs FK VOZDOVAC   2.3 3.2 6.75
  21    LUDOGORETS vs DINAMO MOSCOW     1.2 2.35    2.6
  31    FC THUN vs FC ZURICH            2.9 2.8 2.6
  42    FC NURNBERG vs BOHEMIANS 1905   2.45    2.9 4.6
  53    LILLESTROM vs STROMSGODSET       3.0    2.5 3.35
  64    HALMSTAD vs IF ELFSBORG         2.4 2.5 2.0
  75    FLANDRIA vs CHACARITA JUNIORS   1.78    1.67    2.54
  86    CA RIVER PLATE vs DANUBIO       1.25    1.15    2.7
  97    JUVENTUD vs CERRO               1.55    1.15    2.3

`

是否可以进行单次关节手术

这些表格中存在的数据具有相同的团队值,但在HOMEDRAW& AWAY所以我想要的是一个将执行此规范的SQL语法。

home表格中的myshop值除以1,draw表格中myshop2的值除以1,值away in myshop3表除以1,然后对所有值求和,得到每个团队的一个值,输出应该是相应的团队的总数,如下面的简单例子。

FK PARTIZAN vs CSKA MOSCOW 105

理想的语法应该看起来像

SELECT from MYSHOP(home/1)+MYSHOP2(draw/1)+MYSHOP3(away/1) where TEAMS(MYSHOP=MYSHOP2=MYSHOP3)

需要你的想法。

1 个答案:

答案 0 :(得分:0)

我担心你的语法愿望和语法现实不匹配。

我真的不知道你想要实现的目标,但依赖SPARTAK MOSCOW vs FK VOZDOVAC这样的字符串来加入并不太可能。例如,这些"加入":

  1. SPARTAK MOSCOW vs FK VOZDOVAC
  2. FK VOZDOVAC vs SPARTAK MOSCOW
  3. 将此视为组合这3个表的一种方式:

          SELECT
                1 tbl_src, id_no, teams, home, draw, away
          FROM myshop
          UNION ALL
          SELECT
                2 tbl_src, id_no, teams, home, draw, away
          FROM myshop2
          UNION ALL
          SELECT
                3 tbl_src, id_no, teams, home, draw, away
          FROM myshop3
    

    试试这个:

    SELECT
          teams
        , SUM(home)
        , SUM(draw)
        , SUM(away)
    FROM (
          SELECT
                1 tbl_src, id_no, teams, home, draw, away
          FROM myshop
          UNION ALL
          SELECT
                2 tbl_src, id_no, teams, home, draw, away
          FROM myshop2
          UNION ALL
          SELECT
                3 tbl_src, id_no, teams, home, draw, away
          FROM myshop3
    ) AS d
    GROUP BY
          teams