[英]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,但是它们可能有所不同,因此我尝试检索该组合的所有数据。
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) ))
You neeed to use union or union all , depending on what kind of join you want. 您需要使用union或union all ,这取决于您想要的联接类型。 So for twi qyery results
n1
and n2
: 因此对于twi qyery结果
n1
和n2
:
x < n1+n2
- will remove duplicates x < n1+n2
将删除重复项 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.