简体   繁体   English

如何将这两个查询合并在一起?

[英]How do I merge these two queries together?

SELECT MIN( a.severity ), CONCAT_WS(",", a.dev_id, a.object_id ) AS tuple 
FROM net.alerts AS a INNER JOIN net.deviceinfo AS c ON c.id = a.dev_id AND 
c.peer = ( SELECT value FROM local.settings WHERE setting="server_id" ) WHERE 1 
AND (a.dev_id, a.object_id) IN ( (13,4164),(45,-1),(145,995),(188,-1) ) GROUP BY tuple


SELECT MIN( a.severity ), CONCAT_WS(",", b.device_id, b.object_id ) AS tuple 
FROM net.alerts AS a INNER JOIN local.conditions AS b ON a.condition_id = b.condition_id WHERE 1 
AND(b.device_id, b.object_id) IN ((13,4164),(45,-1),(145,995),(188,-1) ) Group BY tuple

I have these tables, and in the alerts and conditions tables, they all have the device_id and object_id, but they maybe different, so I'm trying to retrieve all data of that composite. 我有这些表,并且在警报和条件表中,它们都具有device_id和object_id,但是它们可能有所不同,因此我尝试检索该组合的所有数据。

I'm not sure I understand what you try to do but I guess you need to use UNION . 我不确定我是否理解您要尝试执行的操作,但是我想您需要使用UNION

By the way why do you write WHERE 1 AND condition and not WHERE condition ? 顺便问一下,为什么要编写WHERE 1 AND condition而不是WHERE condition

Probably it'd be best to work on a single query, but maybe UNION would work for you as you say you just want to merge the result: 也许最好对一个查询进行处理,但是也许UNION会为您工作,因为您说您只想合并结果:

(SELECT MIN( a.severity ), CONCAT_WS(",", a.dev_id, a.object_id ) AS tuple 
FROM net.alerts AS a INNER JOIN net.deviceinfo AS c ON c.id = a.dev_id AND 
c.peer = ( SELECT value FROM local.settings WHERE setting="server_id" ) WHERE 1 AND 
(a.dev_id, a.object_id) IN ( (13,4164),(45,-1),(145,995),(188,-1) ))

UNION 

(SELECT MIN( a.severity ), CONCAT_WS(",", b.device_id, b.object_id ) AS tuple 
FROM net.alerts AS a INNER JOIN local.conditions AS b ON 
a.condition_id = b.condition_id WHERE 1 AND(b.device_id, b.object_id) IN ((13,4164),
(45,-1),(145,995),(188,-1) ))
  • Use "UNION ALL" to allow duplicate values 使用“ UNION ALL”以允许重复值

You neeed to use union or union all , depending on what kind of join you want. 您需要使用unionunion all ,这取决于您想要的联接类型。 So for twi qyery results n1 and n2 : 因此对于twi qyery结果n1n2

  • Union - x < n1+n2 - will remove duplicates 联合x < n1+n2将删除重复项
  • Union All - x == n1+n2 -will save duplicates 全部合并x == n1+n2 n2-将保存重复项

So your code should look like this: 因此,您的代码应如下所示:

(SELECT MIN( a.severity ), CONCAT_WS(",", a.dev_id, a.object_id ) AS tuple 
FROM net.alerts AS a INNER JOIN net.deviceinfo AS c ON c.id = a.dev_id AND 
c.peer = ( SELECT value FROM local.settings WHERE setting="server_id" ) WHERE 1 
AND (a.dev_id, a.object_id) IN ( (13,4164),(45,-1),(145,995),(188,-1) ) GROUP BY tuple)

 UNION

(SELECT MIN( a.severity ), CONCAT_WS(",", b.device_id, b.object_id ) AS tuple 
FROM net.alerts AS a INNER JOIN local.conditions AS b ON a.condition_id = b.condition_id WHERE 1 
AND(b.device_id, b.object_id) IN ((13,4164),(45,-1),(145,995),(188,-1) ) Group BY tuple)

You ned to remember: number and names of your query columns must match exactly. 您要记住:查询列的编号和名称必须完全匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM