所以,在这里完成MYSQL newb。我几乎不好意思发布这个问题,但现在就说了:
所以,我有下表......
CREATE TABLE IF NOT EXISTS cran_cisco (
id int not null auto_increment,
device_fqdn varchar(250) DEFAULT 0,
device_ip varchar(250) DEFAULT 0,
link_state varchar(250) DEFAULT 0,
line_protocol varchar(250) DEFAULT 0,
description varchar(250) DEFAULT 0,
date timestamp default now(),
PRIMARY KEY(id)
);
我创建了一个外部脚本来添加以下信息,每天会有四次。以下是它的外观摘录:
insert into cran_cisco (device_fqdn, device_ip, link_state, line_protocol, description)
values ('test_box_2', '10.10.10.2', 'up', 'up', 'this is a test interface');
insert into cran_cisco (device_fqdn, device_ip, link_state, line_protocol, description)
values ('test_box_2', '10.10.10.2', 'up', 'down', 'this is a test interface');
insert into cran_cisco (device_fqdn, device_ip, link_state, line_protocol, description)
values ('test_box_2', '10.10.10.2', 'up', 'up', 'this is a test interface');
insert into cran_cisco (device_fqdn, device_ip, link_state, line_protocol, description)
values ('test_box_2', '10.10.10.2', 'up', 'down', 'this is a test interface');
insert into cran_cisco (device_fqdn, device_ip, link_state, line_protocol, description)
values ('test_box_2', '10.10.10.2', 'up', 'down', 'this is a test interface');
insert into cran_cisco (device_fqdn, device_ip, link_state, line_protocol, description)
values ('test_box_2', '10.10.10.2', 'up', 'up', 'this is a test interface');
insert into cran_cisco (device_fqdn, device_ip, link_state, line_protocol, description)
values ('test_box_2', '10.10.10.2', 'up', 'up', 'this is a test interface');
insert into cran_cisco (device_fqdn, device_ip, link_state, line_protocol, description)
values ('test_box_2', '10.10.10.2', 'up', 'down', 'this is a test interface');
insert into cran_cisco (device_fqdn, device_ip, link_state, line_protocol, description)
values ('test_box_2', '10.10.10.2', 'up', 'up', 'this is a test interface');
表格看起来像这样:
| id | device_fqdn | device_ip | link_state | line_protocol | description | date |
+----+-------------+------------+------------+---------------+--------------------------+---------------------+
| 1 | test_box_2 | 10.10.10.2 | up | down | this is a test interface | 2016-10-07 08:16:42 |
| 2 | test_box_2 | 10.10.10.2 | up | up | this is a test interface | 2016-10-07 08:17:22 |
| 3 | test_box_2 | 10.10.10.2 | up | up | this is a test interface | 2016-10-07 08:23:55 |
| 4 | test_box_2 | 10.10.10.2 | up | down | this is a test interface | 2016-10-07 08:23:55 |
| 5 | test_box_2 | 10.10.10.2 | up | up | this is a test interface | 2016-10-07 08:23:55 |
| 6 | test_box_2 | 10.10.10.2 | up | up | this is a test interface | 2016-10-07 08:23:55 |
| 7 | test_box_2 | 10.10.10.2 | up | down | this is a test interface | 2016-10-07 08:23:55 |
| 8 | test_box_2 | 10.10.10.2 | up | up | this is a test interface | 2016-10-07 08:23:55 |
| 9 | test_box_2 | 10.10.10.2 | up | up | this is a test interface | 2016-10-07 08:23:55 |
| 10 | test_box_2 | 10.10.10.2 | up | down | this is a test interface | 2016-10-07 08:23:55 |
| 11 | test_box_2 | 10.10.10.2 | up | up | this is a test interface | 2016-10-07 08:23:57 |
+----+-------------+------------+------------+---------------+--------------------------+---------------------+
我想开发一个查询,以便在每次线路协议或链路断开时进行报告。期望输出应该是这样的:
| id | device_fqdn | device_ip | link_state | line_protocol | description | date |
+----+-------------+------------+------------+---------------+--------------------------+---------------------+
| 1 | test_box_2 | 10.10.10.2 | up | down | this is a test interface | 2016-10-07 08:16:42 |
| 4 | test_box_2 | 10.10.10.2 | up | down | this is a test interface | 2016-10-07 08:23:55 |
| 7 | test_box_2 | 10.10.10.2 | up | down | this is a test interface | 2016-10-07 08:23:55 |
| 10 | test_box_2 | 10.10.10.2 | up | down | this is a test interface | 2016-10-07 08:23:55 |
+----+-------------+------------+------------+---------------+--------------------------+---------------------+
非常感谢任何帮助。提前谢谢。
正如所承诺的,我的问题的答案如下。我还有另一个问题,但它正在处理如何将所有这些问题放入bash脚本中。洛尔
SELECT 'cran_juniper' AS `set`, c.*
FROM cran_juniper c
WHERE ROW(c.device_fqdn, c.device_ip, c.interface, c.admin_state, c.link_state, c.description) NOT IN
(
SELECT device_fqdn, device_ip, interface, admin_state, link_state, description
FROM cran_juniper_baseline
)
UNION ALL
SELECT 'cran_juniper_baseline' AS `set`, b.*
FROM cran_juniper_baseline b
WHERE ROW(b.device_fqdn, b.device_ip, b.interface, b.admin_state, b.link_state, b.description) NOT IN
(
SELECT device_fqdn, device_ip, interface, admin_state, link_state, description
FROM cran_juniper
)
into outfile 'today.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
;
答案 0 :(得分:1)
您可以在OR
子句中使用WHERE
条件来检查:
SELECT * from cran_cisco WHERE LINK_STATE = 'down' OR LINE_PROTOCOL = 'down'
如果您正在尝试按照评论中的说明完成结果,则可以尝试按数据和设备进行排序,并创建LAG等效项以跟踪设备首次运行时down
:
set @lags = 'start' ;
set @lagp = 'start' ;
select id, device_fqdn, device_ip, link_state, line_protocol, description, date from (
select *, @lags,@lagp, @lags:=link_state, @lagp := line_protocol from cran_cisco order by device_fqdn, date) c
where (link_state = 'down' and @lags = 'up' ) or ( line_protocol = 'down' and @lagp = 'up')
每个lag
变量显示前一行值。然后,我们只会在link_state
或line_protocol
为down
并且上一行已启动的情况下从该结果中提取。
答案 1 :(得分:0)
希望这有帮助
select * cran_cisco where line_protocol='down' or link_state='down'