mysql union all with aliases,语法错误

时间:2014-07-07 16:30:10

标签: mysql union

为什么我

Error in query (1064): Syntax error near 'as q2)' at line 7

SELECT SQL_NO_CACHE q1.d1, q1.a, q2.b, (q1.a-q2.b)/q1.a*100 as Percentage
FROM 
  (SELECT Date(date) d1, count(id_update) a 
   FROM vas_updates 
   WHERE date > date_sub(now(), interval 2 hour)  
   GROUP BY DATE(date)) as q1
UNION ALL
  (SELECT date(date) as d2, count(id_update) as b 
  FROM vas_updates 
  WHERE date BETWEEN
    date_sub(date_sub(now(), interval 1 day), interval 2 hour) 
    AND
    date_sub(now(), interval 1 day) group by DATE(d2)  ) as q2

我不能在UNION中使用别名吗?

更新: 这个查询可能有来自另一个查询的剩余部分,我想先了解语法错误。

我想要计算的是两个总和的增加或减少的百分比,这是从今天的最后2个小时到与昨天相同的时间段的点击量。

该表格只有iddatetime

3 个答案:

答案 0 :(得分:1)

我怀疑你真的想要一个JOIN

这样的事情: -

SELECT SQL_NO_CACHE q1.d1, q1.a, q2.b, (q1.a-q2.b)/q1.a*100 as Percentage
FROM 
(
    SELECT Date(date) d1, count(id_update) a 
    FROM vas_updates 
    WHERE date > date_sub(now(), interval 2 hour)  
    GROUP BY DATE(date)
) as q1
INNER JOIN
(
    SELECT date(date) as d2, count(id_update) as b 
    FROM vas_updates 
    WHERE date BETWEEN date_sub(date_sub(now(), interval 1 day), interval 2 hour) AND   date_sub(now(), interval 1 day) 
    group by DATE(d2)  
) as q2
ON q1.d1 = q2.d2

修改

检查了您的更新查询,这是您需要的一个加入。

您可以使用CROSS JOIN。您将从每个子查询返回1个值,并对这些值进行计算: -

SELECT SQL_NO_CACHE q1.d1, q1.a, q2.b, (q1.a-q2.b)/q1.a*100 as Percentage
FROM 
(
    SELECT MIN(Date(date)) d1, count(id_update) a 
    FROM vas_updates 
    WHERE date > date_sub(now(), interval 2 hour)
) as q1
CROSS JOIN
(
    SELECT MIN(Date(date)) d2, count(id_update) as b 
    FROM vas_updates 
    WHERE date BETWEEN
    date_sub(date_sub(now(), interval 1 day), interval 2 hour) 
    AND
    date_sub(now(), interval 1 day) 
) as q2

CROSS JOIN为您提供行的每个组合。在这种情况下,您有1个结果记录。我刚刚返回MIN日期以显示一个日期。

答案 1 :(得分:1)

你不能。 UNION操作不允许您在子查询上使用别名,因为它是一个创建单个表的操作。

像这样:

select 1 a, 2 b 
union all
select 3 blah, 4 bleh

这将导致

a     b
1     2
3     4 

请在此处查看:http://sqlfiddle.com/#!2/68b32/444

在此查询中,您只有两个字段,无论第二个查询是什么,它只会解析第一个字段,检查其他查询是否具有与第一个相同的字段数量以及它们是否属于同一类型。使用alies命名UNIONed查询无效。

所以我认为你需要的可能是一个JOIN或者只是所有领域

因此,您的查询将类似于:

SELECT SQL_NO_CACHE tbl.d1, 
                    tbl.a, 
                    tbl.b, 
                    (tbl.a-tbl.b)/tbl.a*100 as Percentage
  FROM  (SELECT Date(date) d1, 
                count(id_update) a,
                null d2,
                null b 
           FROM vas_updates 
          WHERE date > date_sub(now(), interval 2 hour)  
          GROUP BY DATE(date)
         UNION ALL
         SELECT null d1, 
                null a
                date(date) as d2, 
                count(id_update) as b 
           FROM vas_updates 
          WHERE date 
            BETWEEN date_sub(date_sub(now(), interval 1 day), interval 2 hour) 
                AND date_sub(now(), interval 1 day) group by DATE(d2)
         ) tbl

但这很可能不会使计算正确。您可以使用@Kickstart为您提供的版本。

查询@Kickstart的答案

SELECT SQL_NO_CACHE q1.d1, q1.a, q2.b, (q1.a-q2.b)/q1.a*100 as Percentage
FROM 
(
    SELECT Date(date) d1, count(id_update) a 
    FROM vas_updates 
    WHERE date > date_sub(now(), interval 2 hour)  
    GROUP BY DATE(date)
) as q1
INNER JOIN
(
    SELECT date(date) as d2, count(id_update) as b 
    FROM vas_updates 
    WHERE date BETWEEN date_sub(date_sub(now(), interval 1 day), interval 2 hour) AND date_sub(now(), interval 1 day) 
    group by DATE(d2)  
) as q2
ON q1.d1 = q2.d2

我决定用这个答案解释为什么你以错误的方式使用UNION操作。

答案 2 :(得分:1)

我认为@Kickstart是对的,你可以尝试一下。

SELECT SQL_NO_CACHE d, a
FROM 
  (SELECT Date(date) d, count(id_update) as a 
   FROM vas_updates 
   WHERE date > date_senter code hereub(now(), interval 2 hour)  
   GROUP BY DATE(date)) 
UNION ALL
  (SELECT date(date) as d, count(id_update) as a 
  FROM vas_updates 
  WHERE date BETWEEN
    date_sub(date_sub(now(), interval 1 day), interval 2 hour) 
    AND
    date_sub(now(), interval 1 day) group by DATE(d2)  )

我错了,更新,你可以试试这个

SELECT SQL_NO_CACHE q1.d1, q1.a, q2.b, (q1.a-q2.b)/q1.a*100 as Percentage
    FROM 
    (
        SELECT  DATE_FORMAT(Date(date),'%H')  as d1, count(id_update) a 
        FROM vas_updates 
        WHERE date > date_sub(now(), interval 2 hour)  
        GROUP BY DATE(date)
    ) as q1
    INNER JOIN
    (
        SELECT  DATE_FORMAT(Date(date),'%H')    as d2, count(id_update) as b 
        FROM vas_updates 
        WHERE date BETWEEN date_sub(date_sub(now(), interval 1 day), interval 2 hour) AND date_sub(now(), interval 1 day) 
        group by DATE(d2)  
    ) as q2
    ON q1.d1 = q2.d2