2016-10-07 13 views
1

ここでMYSQL newbを完成させてください。私はこの質問を投稿することはほぼ恥ずかしいんだけど、ここではそれが行く:テーブル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) 
); 

そして、私は、以下の情報を追加するために外部スクリプトを作成して、その1日4回になります。

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

私はあなたがそのデバイスの前の行とすべての行を比較し、link_stateおよび/またはline_protocolが正しい、アップからダウンに変更されたかどうか?見てみたいと思いますか – verhie

答えて

1

役立ちます:

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_protocoldownで、前の行が上になった場合にのみ、その結果を引き出します。

Here is an functional example

+0

私の友人は、質問に正しく答えました。私は間違って尋ねた。私は新しいものを掲示するでしょう。どうもありがとうございました。 –

+0

@ AndyD'Arata非常に親切ですが、あなたの問題を解決しましょう;) – EoinS

+0

私はトーンを使って答えを見つけました! ポストの一番下に回答を追加して、私の心に何が流れていたのかを確認します。笑 –

0

・ホープこれはあなたがこれを確認するためにWHERE句でOR条件を使用することができます

select * cran_cisco where line_protocol='down' or link_state='down' 
関連する問題