如何仅当两个字段一致时才在 mysql 数据库中显示结果
How to show results in a mysql database only if two fields in unison are not the same
我有一个 mysql table:
CREATE TABLE tipoUFRubros(
idTipoUFRubro int unsigned not null auto_increment primary key,
idConsorcio int not null,
idRubro int not null,
idTipoUF int not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
这是 table 的内容:
=== idTipoUFRubro === idConsorcio === idRubro ==== idTipoUF
=== 75 =============== 2 =========== 1 =========== 1
=== 76 =============== 2 =========== 2 =========== 1
=== 77 =============== 2 =========== 4 =========== 1
=== 78 =============== 2 =========== 5 =========== 1
=== 79 =============== 2 =========== 6 =========== 1
=== 80 =============== 2 =========== 7 =========== 1
=== 81 =============== 2 =========== 8 =========== 1
=== 82 =============== 2 =========== 9 =========== 1
=== 83 =============== 2 =========== 1 =========== 2
=== 84 =============== 2 =========== 2 =========== 2
=== 85 =============== 2 =========== 3 =========== 2
=== 86 =============== 2 =========== 4 =========== 2
=== 87 =============== 2 =========== 5 =========== 2
=== 88 =============== 2 =========== 6 =========== 2
=== 89 =============== 2 =========== 7 =========== 2
=== 90 =============== 2 =========== 8 =========== 2
=== 91 =============== 2 =========== 9 =========== 2
=== 92 =============== 2 =========== 1 =========== 3
=== 93 =============== 2 =========== 2 =========== 3
=== 94 =============== 2 =========== 4 =========== 3
=== 95 =============== 2 =========== 5 =========== 3
=== 96 =============== 2 =========== 6 =========== 3
=== 97 =============== 2 =========== 7 =========== 3
=== 98 =============== 2 =========== 8 =========== 3
=== 99 =============== 2 =========== 9 =========== 3
=== 100 ============== 2 =========== 10 ========== 3
我想达到的目标:
我正在尝试获取所有具有特定 idRubro
和 idTipoUF
的行(最后两列),但如果 idRubro
已经以不同的 idTipoUF
.
示例:
我只想在 idTipoUF
为 1 时获取 idRubro
1,但是没有 idRubro
1 和 idTipoUF
2 或 3。所以我想列出所有 idRubros仅与 idTipoUF 1 关联。
如果我这样做:
SELECT * FROM tipoUFRubros
WHERE idRubro = 1
AND idTipoUF = 1
AND idTipoUF != 2
我确实得到了 idRubro
1 的结果,即使有一个 idTipoUF
2 也共享 idRubro
1。
我能做什么?我试过没有成功:
SELECT * FROM tipoUFRubros
WHERE idRubro = 1
(AND idTipoUF = 1 AND idTipoUF != 2)
这当然不行。
我需要一个比较,我检查一对不存在。我该怎么做?
您可以使用子查询来检查具有不同 idTipoUF
的行
SELECT *
FROM tipoUFRibros
WHERE idRubro = 1
AND idTipoUF = 1
AND NOT EXISTS (
SELECT *
FROM tipoUFRibros
WHERE idRibro = 1
AND idTipoUF != 1
)
您可以使用 EXISTS 检查您的情况是否存在特殊组合
SELECT *
FROM tipoUFRubros
WHERE idRubro = 1
AND idTipoUF = 1
AND NOT EXISTS (
SELECT *
FROM tipoUFRubros
WHERE idRubro = 1
AND idTipoUF = 2
)
idTipoUFRubro | idConsorcio | idRubro | idTipoUF
------------: | ----------: | ------: | -------:
db<>fiddle here
我有一个 mysql table:
CREATE TABLE tipoUFRubros(
idTipoUFRubro int unsigned not null auto_increment primary key,
idConsorcio int not null,
idRubro int not null,
idTipoUF int not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
这是 table 的内容:
=== idTipoUFRubro === idConsorcio === idRubro ==== idTipoUF
=== 75 =============== 2 =========== 1 =========== 1
=== 76 =============== 2 =========== 2 =========== 1
=== 77 =============== 2 =========== 4 =========== 1
=== 78 =============== 2 =========== 5 =========== 1
=== 79 =============== 2 =========== 6 =========== 1
=== 80 =============== 2 =========== 7 =========== 1
=== 81 =============== 2 =========== 8 =========== 1
=== 82 =============== 2 =========== 9 =========== 1
=== 83 =============== 2 =========== 1 =========== 2
=== 84 =============== 2 =========== 2 =========== 2
=== 85 =============== 2 =========== 3 =========== 2
=== 86 =============== 2 =========== 4 =========== 2
=== 87 =============== 2 =========== 5 =========== 2
=== 88 =============== 2 =========== 6 =========== 2
=== 89 =============== 2 =========== 7 =========== 2
=== 90 =============== 2 =========== 8 =========== 2
=== 91 =============== 2 =========== 9 =========== 2
=== 92 =============== 2 =========== 1 =========== 3
=== 93 =============== 2 =========== 2 =========== 3
=== 94 =============== 2 =========== 4 =========== 3
=== 95 =============== 2 =========== 5 =========== 3
=== 96 =============== 2 =========== 6 =========== 3
=== 97 =============== 2 =========== 7 =========== 3
=== 98 =============== 2 =========== 8 =========== 3
=== 99 =============== 2 =========== 9 =========== 3
=== 100 ============== 2 =========== 10 ========== 3
我想达到的目标:
我正在尝试获取所有具有特定 idRubro
和 idTipoUF
的行(最后两列),但如果 idRubro
已经以不同的 idTipoUF
.
示例:
我只想在 idTipoUF
为 1 时获取 idRubro
1,但是没有 idRubro
1 和 idTipoUF
2 或 3。所以我想列出所有 idRubros仅与 idTipoUF 1 关联。
如果我这样做:
SELECT * FROM tipoUFRubros
WHERE idRubro = 1
AND idTipoUF = 1
AND idTipoUF != 2
我确实得到了 idRubro
1 的结果,即使有一个 idTipoUF
2 也共享 idRubro
1。
我能做什么?我试过没有成功:
SELECT * FROM tipoUFRubros
WHERE idRubro = 1
(AND idTipoUF = 1 AND idTipoUF != 2)
这当然不行。
我需要一个比较,我检查一对不存在。我该怎么做?
您可以使用子查询来检查具有不同 idTipoUF
SELECT *
FROM tipoUFRibros
WHERE idRubro = 1
AND idTipoUF = 1
AND NOT EXISTS (
SELECT *
FROM tipoUFRibros
WHERE idRibro = 1
AND idTipoUF != 1
)
您可以使用 EXISTS 检查您的情况是否存在特殊组合
SELECT * FROM tipoUFRubros WHERE idRubro = 1 AND idTipoUF = 1 AND NOT EXISTS ( SELECT * FROM tipoUFRubros WHERE idRubro = 1 AND idTipoUF = 2 )
idTipoUFRubro | idConsorcio | idRubro | idTipoUF ------------: | ----------: | ------: | -------:
db<>fiddle here