mysql连接多个select语句的语法错误

时间:2014-07-31 16:18:47

标签: mysql select join statements

我有两个我想加入的选择语句。另外,它们工作正常,一起mysql告诉我,我有一个语法错误,我不知道在哪里。 查询1是:

select * from(
select items.hostid, trends_uint.itemid,
avg(trends_uint.`value_avg`)/1024/1024/1024 as Average_Used, clock,
date_format(from_unixtime(trends_uint.`clock`), '%Y-%m') AS report_date from trends_uint, items
where (trends_uint.itemid = 75283  and items.hostid=10222) 
group by trends_uint.itemid, report_date) 
as used; 

+--------+--------+-----------------------+------------+-------------+
| hostid | itemid | Average_Used          | clock      | report_date |
+--------+--------+-----------------------+------------+-------------+
|  10222 |  75283 | 1764.8172729810664676 | 1403344800 | 2014-06     |
|  10222 |  75283 | 1792.1519809950560109 | 1404190800 | 2014-07     |
+--------+--------+-----------------------+------------+-------------+

查询2是:

select * from (select items.hostid, trends_uint.itemid,
avg(trends_uint.`value_avg`)/1024/1024/1024 as Space_Allocated, clock,
date_format(from_unixtime(trends_uint.`clock`), '%Y-%m') AS report_date from trends_uint, items
where (trends_uint.itemid = 75281  and items.hostid=10222) 
group by trends_uint.itemid, report_date) as allocated;

+--------+--------+-----------------------+------------+-------------+
| hostid | itemid | Space_Allocated       | clock      | report_date |
+--------+--------+-----------------------+------------+-------------+
|  10222 |  75281 | 2432.0000000000000000 | 1403344800 | 2014-06     |
|  10222 |  75281 | 2432.0000000000000000 | 1404190800 | 2014-07     |
+--------+--------+-----------------------+------------+-------------+

我尝试加入:

select * from(
select items.hostid, trends_uint.itemid,
avg(trends_uint.`value_avg`)/1024/1024/1024 as Average_Used, clock,
date_format(from_unixtime(trends_uint.`clock`), '%Y-%m') AS report_date from trends_uint, items
where (trends_uint.itemid = 75283  and items.hostid=10222) 
group by trends_uint.itemid, report_date) 
as used

join

select * from (select items.hostid, trends_uint.itemid,
avg(trends_uint.`value_avg`)/1024/1024/1024 as Space_Allocated, clock,
date_format(from_unixtime(trends_uint.`clock`), '%Y-%m') AS report_date from trends_uint, items
where (trends_uint.itemid = 75281  and items.hostid=10222) 
group by trends_uint.itemid, report_date) as allocated
on allocated.report_date=used.report_date;

2 个答案:

答案 0 :(得分:0)

语法错误是外部查询中有两个SELECT个关键字。

您已形成如下查询:

SELECT * FROM (SELECT ...) AS used
JOIN SELECT * FROM (SELECT ...) AS allocated ON ...

相反,它需要像:

SELECT * FROM (SELECT ...) AS used
JOIN (SELECT ...) AS allocated ON ...

以这种方式思考:JOIN的操作数是表引用,而不是SELECT的实例。

答案 1 :(得分:0)

我认为你错过了一个支架

SELECT * 
  FROM
  (
    SELECT items.hostid, trends_uint.itemid,
    AVG(trends_uint.`value_avg`)/1024/1024/1024 AS Average_Used, clock,
    DATE_FORMAT(FROM_UNIXTIME(trends_uint.`clock`), '%Y-%m') AS report_date 
    FROM trends_uint, items
    WHERE (trends_uint.itemid = 75283  AND items.hostid=10222) 
    GROUP BY trends_uint.itemid, report_date
    ) AS used
  JOIN
  (
    SELECT * FROM (SELECT items.hostid, trends_uint.itemid,
    AVG(trends_uint.`value_avg`)/1024/1024/1024 AS Space_Allocated, clock,
    DATE_FORMAT(FROM_UNIXTIME(trends_uint.`clock`), '%Y-%m') AS report_date 
    FROM trends_uint, items
    WHERE (trends_uint.itemid = 75281  AND items.hostid=10222) 
    GROUP BY trends_uint.itemid, report_date
    ) AS allocated
ON allocated.report_date=used.report_date;