MySql Combine two queries (subselect or join or union)

我只是一个虾纸丫 提交于 2019-12-11 07:46:14

问题


I have two working queries that I would like to combine to provide one lot of output for a wall display. I am NOT a DB person by any means but have managed to create these queries from scratch, albeit with a lot of info from here!

I have four tables with relevant columns:

Hosts[host, hostid]
Items[hostid, itemid, itemname]
History_unit[itemid, value]
History_log[itemid, value]

hostid and itemid are our identifiers The history_xxx tables are just that, and entry for every record of that data.

After trying to combine these for too many hours I just don't understand enough to make it work.

Query 1

SELECT hosts.host, 
max((case when items.name='RP_Dayend_OK' then history_uint.value end) *1000) as 'Day End', 
max((case when items.name='RP_Sync_OK' then history_uint.value end) *1000) as 'Sync',
max((case when items.name='RP_Monthend_OK' then history_uint.value end) *1000) as 'Month End', 
max(case when items.name='RP_Version' then history_uint.value end) as 'Version'
from hosts, items, history_uint
where hosts.hostid=items.hostid and items.itemid=history_uint.itemid and items.name like '%RP\_%'
group by hosts.host

Output:

Host    | Day End | Sync | Month End | Version
Host 1  | date    | date | date      | 7xx
Host 2  | date    | date | date      | 7xx

Query 2

SELECT hosts.host, 
max(case when items.name='RP_Cron' then history_log.value end) as 'cron'
from hosts, items, history_log
where hosts.hostid=items.hostid and items.itemid=history_log.itemid and items.name like '%RP\_%'
group by hosts.host

Output:

Host    | Cron
Host 1  | string     
Host 2  | string

What I would like is:

Host    | Day End | Sync | Month End | Version | Cron
Host 1  | date    | date | date      | 7xx     | string
Host 2  | date    | date | date      | 7xx     | string

I did manage a sub select but I ended up with a different row for each host for each item, and no data for 'cron'. I also tried joins to no avail. It is simply my lack of understanding.

Thanks for any help!


回答1:


You should abandon the use of implicit (comma separated) joins in favour of explicit joins. In your case LEFT (outer) joins are appropriate.

DROP  TABLE  IF EXISTS  Hosts;
DROP  TABLE  IF EXISTS  Items;
DROP  TABLE  IF EXISTS  History_unit;
DROP  TABLE  IF EXISTS  History_uint;
DROP  TABLE  IF EXISTS  History_log;

CREATE TABLE Hosts(host VARCHAR(20), hostid INT);
CREATE TABLE Items(hostid INT, itemid INT, name VARCHAR(20));
CREATE TABLE History_uint(itemid INT, value INT);
CREATE TABLE History_log(itemid INT, value INT);

INSERT INTO HOSTS VALUES ('HOST1',1),('HOST2',2);
INSERT INTO ITEMS VALUES
(1,1,'RP_Dayend_OK'),
(1,2,'RP_Sync_OK'),
(1,3,'RP_Monthend_OK'),
(1,4,'RP_Version' ),
(2,1,'RP_Dayend_OK'),
(2,2,'RP_Sync_OK'),
(2,2,'RP_cron')
;
INSERT INTO HISTORY_uint VALUES
(1,10),(2,10),(3,10),(4,10),
(1,50),(3,60);

INSERT INTO HISTORY_log VALUES
(1,10),(2,10),(3,10),(4,10)
;

SELECT hosts.host, 
max((case when items.name='RP_Dayend_OK' then history_uint.value end) *1000) as 'Day End', 
max((case when items.name='RP_Sync_OK' then history_uint.value end) *1000) as 'Sync',
max((case when items.name='RP_Monthend_OK' then history_uint.value end) *1000) as 'Month End', 
max(case when items.name='RP_Version' then history_uint.value end) as 'Version',
max(case when items.name='RP_Cron' then history_log.value end) as 'cron'
from hosts
left join  items on items.hostid = hosts.hostid
left join  history_uint on history_uint.itemid = items.itemid
left join  history_log on history_log.itemid = items.itemid
where items.name like '%RP\_%'
group by hosts.host;

+-------+---------+-------+-----------+---------+------+
| host  | Day End | Sync  | Month End | Version | cron |
+-------+---------+-------+-----------+---------+------+
| HOST1 |   50000 | 10000 |     60000 |      10 | NULL |
| HOST2 |   50000 | 10000 |      NULL |    NULL |   10 |
+-------+---------+-------+-----------+---------+------+
2 rows in set (0.00 sec)

Note it's usually best if the OP provides the data.



来源:https://stackoverflow.com/questions/57156247/mysql-combine-two-queries-subselect-or-join-or-union

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!