将多个mysql查询组合到1个输出

时间:2018-01-13 18:57:47

标签: mysql union

我在这里做错了什么,因为我试图从1个查询中得到以下两个查询的所有输出,即

计数(centerpoint_stream_stable),日期,website_online,icecast_source_online,icecast_source_ip,icecast_no_listeners,centerpoint_online,centerpoint_connection,centerpoint_stream_stable,centerpoint_stream_status,horsleypark_online,horsleypark_connection,horsleypark_stream_stable,horsleypark_stream_status,local_primary_internet_online,local_primary_internet_ping,local_primary_instreamer_online,local_secondary_internet_online,local_secondary_internet_ping,local_secondary_instreamer_online,system_ok 我希望加入的2个查询是

select count(centerpoint_stream_stable) from status_log where  centerpoint_stream_stable = 'Disconnected/ Reconnected to Stream' and  date > date_sub(now(), interval 1 minute) ;

SELECT date, website_online, icecast_source_online, icecast_source_ip, icecast_no_listeners, centerpoint_online, centerpoint_connection, centerpoint_stream_stable,centerpoint_stream_status,  horsleypark_online, horsleypark_connection,horsleypark_stream_stable,horsleypark_stream_status, local_primary_internet_online,local_primary_internet_ping, local_primary_instreamer_online,local_secondary_internet_online,local_secondary_internet_ping,local_secondary_instreamer_online,system_ok FROM status_log ORDER BY id DESC LIMIT 1;

以上两个查询的并集给出了以下错误;

  

错误1248(42000):每个派生表必须有自己的别名

(select * from (select 
        date, 
    website_online, 
    icecast_source_online, 
    icecast_source_ip, 
    icecast_no_listeners, 
    centerpoint_online, 
    centerpoint_connection,     
    centerpoint_stream_stable,
    centerpoint_stream_status,      
    horsleypark_online, 
    horsleypark_connection,
    horsleypark_stream_stable,
    horsleypark_stream_status, 
    local_primary_internet_online,
    local_primary_internet_ping,        
    local_primary_instreamer_online,    
    local_secondary_internet_online,    
    local_secondary_internet_ping,
    local_secondary_instreamer_online,
    system_ok
FROM status_log ORDER BY id DESC LIMIT 1))


union all
(select * from (select count(centerpoint_stream_stable) from status_log where  centerpoint_stream_stable = 'Disconnected/ Reconnected to Stream' and  date > date_sub(now(), interval 1 minute) ));

1 个答案:

答案 0 :(得分:0)

括号用于子查询,然后您必须为每个子查询指定一个别名,就像错误所暗示的那样。例如:

(SELECT id FROM table1) a
UNION
(SELECT id FROM table2) b

更重要的是,当您使用UNION时,两个查询中的所有字段都必须完全匹配。您的查询具有完全不同的字段。

编辑 UNION是将第一个查询的结果添加到第二个查询的结果中。在您的情况下,您不是要尝试将结果添加到一起,而是尝试将字段添加到一起。为此,您可以使用连接或子查询。尝试使用子查询:

SELECT (SELECT COUNT (s2.centerpoint_stream_stable)
        FROM status_log s2
        WHERE s2.id = s.id
          AND s2.centerpoint_stream_stable = 'Disconnected/ Reconnected to Stream'
          AND s2.date > date_sub(NOW(), INTERVAL 1 MINUTE)) AS my_count,

       s.date, s.website_online, s.icecast_source_online, s.icecast_source_ip,
       s.icecast_no_listeners, s.centerpoint_online, s.centerpoint_connection,
       s.centerpoint_stream_stable, s.centerpoint_stream_status,  s.horsleypark_online,
       s.horsleypark_connection, s.horsleypark_stream_stable, s.horsleypark_stream_status,
       s.local_primary_internet_online, s.local_primary_internet_ping,
       s.local_primary_instreamer_online, s.local_secondary_internet_online,
       s.local_secondary_internet_ping, s.local_secondary_instreamer_online, s.system_ok
FROM status_log s
ORDER BY id DESC
LIMIT 1;

我将您的第一个查询作为子查询。它为您提供了第一个计数字段。我称之为my_count,但您可以将其更改为您喜欢的任何内容。

为了将两个查询连接在一起,您需要提供加入条件,因此我将条件s2.id = s.id添加到您的第一个查询中。

我不知道你为什么在第二次查询结束时LIMIT 1。这将为您提供一条记录最大的id