查找具有相同 ID 且具有一组特定名称的行
编辑:
我有一个像这样有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是将每个 ID 的 COUNT 与列表中的元素数量进行比较:
注意:不需要 ORDER BY,如果需要,则在 HAVING 之后进行
编辑,并更新问题。在MySQL中,使用单独的表来存储搜索词会更容易
编辑2,无需额外的表,请使用派生(内联)表:
The simplest way is to compare a COUNT per ID with the number of elements in your list:
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
Edit 2, without extra table, use a derived (inline) table:
与 gbn 类似,但允许重复 ID/名称组合的可能性:
Similar to gbn, but allowing for the possibility of duplicate ID/Name combinations:
好的!...我解决了我的问题!我修改了 GBN 的逻辑,使其无需使用 IN 子句
1 的搜索表即可完成 MAX(rev) = MIN(REV) 的缺陷:如果我有这样的数据。
然后,当我使用类似的查询时,
它不会显示 ID 1,因为最小值和最大值不同,并且计数为 3。
因此,我只需向 groupby 添加另一列,
这样最终的查询就非常
感谢,感谢所有帮助。 !
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 .
then when I use a query like
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
Thanks,to all that helped. !