查找具有相同 ID 且具有一组特定名称的行

发布于 2024-12-23 04:05:45 字数 405 浏览 1 评论 0原文

编辑:

我有一个像这样有 3 行的表。

ID    NAME REV
1     A    0
1     B    0
1     C    0
2     A    1
2     B    0
2     C    0
3     A    1
3     B    1

我想找到具有一组特定名称且 REV 相同的 ID 例子: Edit2:GBN 的解决方案本来可以完美地工作,但因为我无权创建新表。添加的约束是不能创建新表。

if input = A,B then output is 3
if input = A ,B,C then output is 1 and not 1,2 since the rev level differs in 2.

EDIT:

I have a table with 3 rows like so.

ID    NAME REV
1     A    0
1     B    0
1     C    0
2     A    1
2     B    0
2     C    0
3     A    1
3     B    1

I want to find the ID wich has a particular set of Names and the REV is same
example:
Edit2: GBN's solution would have worked perfectly, but since i do not have the access to create new tables. The added constraint is that no new tables can be created.

if input = A,B then output is 3
if input = A ,B,C then output is 1 and not 1,2 since the rev level differs in 2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

决绝 2024-12-30 04:05:45

最简单的方法是将每个 ID 的 COUNT 与列表中的元素数量进行比较:

SELECT
   ID
FROM
   MyTable
WHERE
   NAME IN ('A', 'B', 'C')
GROUP BY
   ID
HAVING
   COUNT(*) = 3;

注意:不需要 ORDER BY,如果需要,则在 HAVING 之后进行

编辑,并更新问题。在MySQL中,使用单独的表来存储搜索词会更容易

DROP TABLE IF EXISTS gbn;
CREATE TABLE gbn (ID INT, `name` VARCHAR(100), REV INT);
INSERT gbn VALUES (1, 'A', 0);
INSERT gbn VALUES (1, 'B', 0);
INSERT gbn VALUES (1, 'C', 0);
INSERT gbn VALUES (2, 'A', 1);
INSERT gbn VALUES (2, 'B', 0);
INSERT gbn VALUES (2, 'C', 0);
INSERT gbn VALUES (3, 'A', 0);
INSERT gbn VALUES (3, 'B', 0);

DROP TABLE IF EXISTS gbn1;
CREATE TABLE gbn1 ( `name` VARCHAR(100));
INSERT gbn1 VALUES ('A');
INSERT gbn1 VALUES ('B');

SELECT
   gbn.ID
FROM
   gbn
   LEFT JOIN
   gbn1 ON gbn.`name` = gbn1.`name`
GROUP BY
   gbn.ID
HAVING
   COUNT(*) = (SELECT COUNT(*) FROM gbn1)
   AND MIN(gbn.REV) = MAX(gbn.REV);

INSERT gbn1 VALUES ('C');

SELECT
   gbn.ID
FROM
   gbn
   LEFT JOIN
   gbn1 ON gbn.`name` = gbn1.`name`
GROUP BY
   gbn.ID
HAVING
   COUNT(*) = (SELECT COUNT(*) FROM gbn1)
   AND MIN(gbn.REV) = MAX(gbn.REV);

编辑2,无需额外的表,请使用派生(内联)表:

SELECT
   gbn.ID
FROM
   gbn
   LEFT JOIN
   (SELECT 'A' AS `name`
    UNION ALL SELECT 'B' 
    UNION ALL SELECT 'C'
   ) gbn1 ON gbn.`name` = gbn1.`name`
GROUP BY
   gbn.ID
HAVING
   COUNT(*) = 3 -- matches number of elements in gbn1 derived table
   AND MIN(gbn.REV) = MAX(gbn.REV);

The simplest way is to compare a COUNT per ID with the number of elements in your list:

SELECT
   ID
FROM
   MyTable
WHERE
   NAME IN ('A', 'B', 'C')
GROUP BY
   ID
HAVING
   COUNT(*) = 3;

Note: ORDER BY isn't needed and goes after the HAVING if needed

Edit, with question update. In MySQL, it's easier to use a separate table for search terms

DROP TABLE IF EXISTS gbn;
CREATE TABLE gbn (ID INT, `name` VARCHAR(100), REV INT);
INSERT gbn VALUES (1, 'A', 0);
INSERT gbn VALUES (1, 'B', 0);
INSERT gbn VALUES (1, 'C', 0);
INSERT gbn VALUES (2, 'A', 1);
INSERT gbn VALUES (2, 'B', 0);
INSERT gbn VALUES (2, 'C', 0);
INSERT gbn VALUES (3, 'A', 0);
INSERT gbn VALUES (3, 'B', 0);

DROP TABLE IF EXISTS gbn1;
CREATE TABLE gbn1 ( `name` VARCHAR(100));
INSERT gbn1 VALUES ('A');
INSERT gbn1 VALUES ('B');

SELECT
   gbn.ID
FROM
   gbn
   LEFT JOIN
   gbn1 ON gbn.`name` = gbn1.`name`
GROUP BY
   gbn.ID
HAVING
   COUNT(*) = (SELECT COUNT(*) FROM gbn1)
   AND MIN(gbn.REV) = MAX(gbn.REV);

INSERT gbn1 VALUES ('C');

SELECT
   gbn.ID
FROM
   gbn
   LEFT JOIN
   gbn1 ON gbn.`name` = gbn1.`name`
GROUP BY
   gbn.ID
HAVING
   COUNT(*) = (SELECT COUNT(*) FROM gbn1)
   AND MIN(gbn.REV) = MAX(gbn.REV);

Edit 2, without extra table, use a derived (inline) table:

SELECT
   gbn.ID
FROM
   gbn
   LEFT JOIN
   (SELECT 'A' AS `name`
    UNION ALL SELECT 'B' 
    UNION ALL SELECT 'C'
   ) gbn1 ON gbn.`name` = gbn1.`name`
GROUP BY
   gbn.ID
HAVING
   COUNT(*) = 3 -- matches number of elements in gbn1 derived table
   AND MIN(gbn.REV) = MAX(gbn.REV);
雪花飘飘的天空 2024-12-30 04:05:45

与 gbn 类似,但允许重复 ID/名称组合的可能性:

SELECT ID
FROM MyTable
WHERE NAME IN ('A', 'B', 'C')
GROUP BY ID
HAVING COUNT(DISTINCT NAME) = 3;

Similar to gbn, but allowing for the possibility of duplicate ID/Name combinations:

SELECT ID
FROM MyTable
WHERE NAME IN ('A', 'B', 'C')
GROUP BY ID
HAVING COUNT(DISTINCT NAME) = 3;
一萌ing 2024-12-30 04:05:45

好的!...我解决了我的问题!我修改了 GBN 的逻辑,使其无需使用 IN 子句

1 的搜索表即可完成 MAX(rev) = MIN(REV) 的缺陷:如果我有这样的数据。

ID    NAME   REV
1     A      0
1     B      1
1     A      1

然后,当我使用类似的查询时,

Select ID from TABLE
where NAME in {A,B}
groupby ID
having count(*) = 2
and MIN(REV) = MAX(REV)

它不会显示 ID 1,因为最小值和最大值不同,并且计数为 3。

因此,我只需向 groupby 添加另一列,

这样最终的查询就非常

Select ID from TABLE
where NAME in {A,B}
groupby ID,REV
having count(*) = 2
and MIN(REV) = MAX(REV)

感谢,感谢所有帮助。 !

OKAY!... I solved my problem ! I modified GBN's logic to do it without a search table using the IN clause

1 flaw with doing MAX(rev) = MIN(REV) is: if i have a data like so .

ID    NAME   REV
1     A      0
1     B      1
1     A      1

then when I use a query like

Select ID from TABLE
where NAME in {A,B}
groupby ID
having count(*) = 2
and MIN(REV) = MAX(REV)

it will not show me the ID 1 as the min and max are different and the count is 3.

So i simply add another column to the groupby

so the final query is

Select ID from TABLE
where NAME in {A,B}
groupby ID,REV
having count(*) = 2
and MIN(REV) = MAX(REV)

Thanks,to all that helped. !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文