Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 比较表的行和输出的差异_Mysql - Fatal编程技术网

Mysql 比较表的行和输出的差异

Mysql 比较表的行和输出的差异,mysql,Mysql,所以,在这里完成MYSQL新版本。我几乎不好意思发布这个问题,但这里是这样的: 所以,我有下表 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

所以,在这里完成MYSQL新版本。我几乎不好意思发布这个问题,但这里是这样的:

所以,我有下表

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 |
+----+-------------+------------+------------+---------------+--------------------------+---------------------+
我想开发一个查询,以便在线路协议或链路每次中断时报告。expect输出应该如下所示:

| 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';
;
希望这有帮助

select * cran_cisco where line_protocol='down' or link_state='down'

您可以在
WHERE
子句中使用
条件来检查:

SELECT * from cran_cisco WHERE LINK_STATE = 'down' OR LINE_PROTOCOL = '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\u state
line\u protocol
down
且前一行为up时从该结果中提取


我的朋友,你回答得对。我问错了。我会贴一个新的。非常感谢。@AndyD'Arata非常感谢您,但让我们来解决您的问题;)我通过大量的研究找到了答案!我会在帖子的底部添加答案,这样你就可以看到我的想法了。lolI猜想您想将该设备的每一行与前一行进行比较,看看链路状态和/或线路协议是否从上到下发生了变化,对吗?