MySQL加入2个查询

时间:2015-11-01 07:35:22

标签: mysql

我正在尝试取得

的结果
SELECT SUM(pont) AS totalpont
      FROM
    (SELECT jogador.pont from jogador,usuarios WHERE email='like' and jogador.id=usuarios.j1 
    UNION 
    SELECT jogador.pont from jogador,usuarios WHERE email='like' and jogador.id=usuarios.j2
    UNION
    SELECT jogador.pont from jogador,usuarios WHERE email='like' and jogador.id=usuarios.j3 
    UNION
    SELECT jogador.pont from jogador,usuarios WHERE email='like' and jogador.id=usuarios.j4) T

SELECT mercado FROM variaveis 

进入相同的查询,两个独立的数据集并排,例如,查询1返回1,2,3,4,5,查询2返回A,B,C。我希望结果为1,A,2,B,3,C,4,null,5,null

1 个答案:

答案 0 :(得分:0)

我希望您需要合并这些查询并需要一个SUM,因为您可以使用' AS'语法(将mercado重命名为pont),这样你就可以求和。

SELECT SUM(COALESCE(pont, 0)) as totalpont, SUM(COALESCE(mercado, 0)) AS totalmercado
      FROM
    (SELECT jogador.pont, NULL AS mercado from jogador,usuarios WHERE email='like' and jogador.id=usuarios.j1 
    UNION 
    SELECT jogador.pont, NULL AS mercado from jogador,usuarios WHERE email='like' and jogador.id=usuarios.j2
    UNION
    SELECT jogador.pont, NULL AS mercado from jogador,usuarios WHERE email='like' and jogador.id=usuarios.j3 
    UNION
    SELECT jogador.pont, NULL AS mercado from jogador,usuarios WHERE email='like' and jogador.id=usuarios.j4) T
    UNION
    SELECT NULL AS pont, mercado  FROM variaveis )T