2

有两张桌子

表格1

select * from manage_tcp;
+----+----------+--------+
| id | services | statut |
+----+----------+--------+
|  2 |     1433 | up     |
|  2 |     3389 | up     |
|  3 |     3389 | down   |
|  3 |     1433 | down   |
|  9 |     3389 | up     |
|  8 |     3389 | up     |
|  7 |     1433 | up     |
|  6 |     3389 | up     |
|  5 |     3389 | up     |
|  4 |     3389 | up     |
| 10 |     1433 | up     |
| 11 |     1433 | up     |
| 12 |     3389 | up     |
| 13 |     1433 | up     |
| 14 |     3389 | up     |
| 15 |     1433 | up     |
| 16 |     3389 | up     |
| 17 |     1433 | up     |
| 18 |     3389 | up     |
| 19 |     1433 | up     |
| 20 |     3389 | up     |
| 21 |     1433 | up     |
| 24 |     1433 | up     |
| 23 |     3389 | up     |
| 25 |     3389 | up     |
| 26 |     1433 | up     |
| 29 |     1433 | up     |
| 28 |     3389 | up     |
| 30 |     3389 | up     |
| 31 |       80 | up     |
| 32 |     3389 | up     |
| 33 |       80 | up     |
| 34 |     3389 | up     |
| 35 |       80 | up     |
+----+----------+--------+
34 rows in set (0.00 sec)

表 2

mysql> select * from manage_host;
+----+------------+-------------+--------+-------+--------------+----------+------+
| id | uptime     | type        | statut | group | thresold_ref | thresold | mail |
+----+------------+-------------+--------+-------+--------------+----------+------+
|  2 |  277248529 | win2003.png | up     |     4 |            1 |        0 | NULL |
|  3 |  277277471 | win2003.png | prob   |     4 |            1 |        0 | NULL |
|  4 |  346159833 | win2003.png | up     |     4 |            1 |        0 | NULL |
|  5 |  930205491 | win2003.png | up     |     5 |            1 |        0 | NULL |
|  6 | 3805663007 | win2003.png | up     |     5 |            1 |        0 | NULL |
|  7 |         -1 | win2003.png | up     |     4 |            1 |        0 | NULL |
|  8 |   29413867 | win2003.png | up     |     5 |            1 |        0 | NULL |
|  9 |  981986401 | win2003.png | up     |     5 |            1 |        0 | NULL |
| 10 |         -1 | win2003.png | up     |     5 |            1 |        0 | NULL |
| 11 |         -1 | win2003.png | up     |     5 |            1 |        0 | NULL |
| 12 | 2230787611 | win2003.png | up     |     7 |            1 |        0 | NULL |
| 13 |         -1 | win2003.png | up     |     7 |            1 |        0 | NULL |
| 14 | 1004161923 | win2003.png | up     |     8 |            1 |        0 | NULL |
| 15 |         -1 | win2003.png | up     |     8 |            1 |        0 | NULL |
| 16 | 1592294954 | win2003.png | up     |     8 |            1 |        0 | NULL |
| 17 |         -1 | win2003.png | up     |     8 |            1 |        0 | NULL |
| 18 | 1449216250 | win2003.png | up     |     4 |            1 |        0 | NULL |
| 19 |         -1 | win2003.png | up     |     4 |            1 |        0 | NULL |
| 20 | 3461945234 | win2003.png | up     |     4 |            1 |        0 | NULL |
| 21 |         -1 | win2003.png | up     |     4 |            1 |        0 | NULL |
| 23 | 3461946968 | win2003.png | up     |     4 |            1 |        0 | NULL |
| 24 |         -1 | win2003.png | up     |     4 |            1 |        0 | NULL |
| 25 | 3462022562 | win2003.png | up     |     4 |            1 |        0 | NULL |
| 26 |         -1 | win2003.png | up     |     4 |            1 |        0 | NULL |
| 35 | 3318333401 | win2003.png | up     |     6 |            1 |        0 |      |
| 28 | 3461879984 | win2003.png | up     |     4 |            1 |        0 | NULL |
| 29 |         -1 | win2003.png | up     |     4 |            1 |        0 | NULL |
| 30 | 1872950109 | win2003.png | up     |     6 |            1 |        0 | NULL |
| 31 | 1872950125 | win2003.png | up     |     6 |            1 |        0 | NULL |
| 32 | 1955232033 | win2003.png | up     |     6 |            1 |        0 | NULL |
| 34 | 3318333401 | win2003.png | up     |     6 |            1 |        0 |      |
| 33 | 1955232049 | win2003.png | up     |     6 |            1 |        0 | NULL |
+----+------------+-------------+--------+-------+--------------+----------+------+
32 rows in set (0.00 sec)

我想比较上面的这两个表,并在表 1 中得到结果,而不是在表 2 中,例如:(这是我想要的结果

+----+----------+--------+
| id | services | statut |
+----+----------+--------+
|  2 |     1433 | up     |
|  3 |     3389 | down   |
|  3 |     1433 | down   |
+----+----------+--------+

我试过了

SELECT a.*,b.* 
FROM manage_tcp as a 
LEFT JOIN manage_host as b 
USING(id,statut) 
WHERE b.id is null;

但我得到了

+----+----------+--------+
| id | services | statut |
+----+----------+--------+
|  3 |     3389 | down   |
|  3 |     1433 | down   |
+----+----------+--------+

这不是我想要的。

非常感谢!

4

3 回答 3

4

尝试这个:

SELECT m.id, m.services, m.statut 
FROM manage_tcp m
WHERE EXISTS 
(
    SELECT * 
    FROM manage_host mh 
    WHERE mh.id = m.id AND m.statut = mh.statut
)

这将为您提供表格的交集manage_tcp以及manage_host基于idstatus列。

得到表 1 中的结果,而不是表 2 中的结果

如果您需要拖车表之间的差异,请使用NOT EXISTS而不是EXISTS这样:

SELECT m.id, m.services, m.statut 
FROM manage_tcp m
WHERE NOT EXISTS 
(
    SELECT * 
    FROM manage_host mh 
    WHERE mh.id = m.id AND m.statut = mh.statut 
)

通常,这是SQL 标准定义为集合交集和差集运算符的 and 运算符INTERSECT的实现。EXCEPT但是,不幸的是,大多数数据库系统并没有实现这些运算符。MS SQL Server 确实支持这些运算符,但不支持 MYSQL。

于 2012-06-20T09:11:46.710 回答
3

试试这个:(假设比较列是idstatut

SELECT a.id, a.services, a.statut
    FROM manage_tcp a
    WHERE NOT EXISTS(SELECT b.id FROM manage_host b WHERE b.id=a.id AND b.statut=a.statut)
UNION
SELECT a.id, (SELECT services FROM manage_tcp WHERE id=a.id ORDER BY id LIMIT 1), a.statut
    FROM manage_tcp a 
    GROUP BY a.id, a.statut HAVING COUNT(1)>1


第一个查询将产生表 a 中不在表 b 中的结果。

第二个,在 UNION 之后,将为在表 a 中出现两次的 id 生成结果。

于 2012-06-20T10:17:06.270 回答
3
SELECT manage_tcp.* FROM 
(
    SELECT a.id,a.statut,a.cnt
    FROM (
        SELECT id, statut, COUNT(1) AS cnt FROM manage_tcp GROUP BY id,statut
    ) AS a
    LEFT JOIN manage_host AS b USING(id, statut)
    GROUP BY a.id, a.statut
    HAVING a.cnt > COUNT(1)
) AS diff
LEFT JOIN manage_tcp USING(id,statut)

或者

SELECT a.id,a.statut,a.services,a.cnt
FROM (
    SELECT id,statut,services,COUNT(1) AS cnt FROM manage_tcp GROUP BY id,statut
) AS a
LEFT JOIN manage_host AS b USING(id, statut)
GROUP BY a.id,a.statut
HAVING a.cnt > COUNT(1)
于 2012-06-20T09:40:01.043 回答