在重复输入的情况下获取特定的输入 [英] Get specific entry in case of duplicate entry
问题描述
情况: 我有一种方案,其中除一个字段外,所有字段的数据都相同.我编写了一个存储过程,使用来自不同表的许多联接来获取数据.就我而言,我需要根据情况选择两种情况之一. 例如:
Situation: I have a scenario in which I get data with all the fields same except just one field. I have written a store procedure that gets the data, using many join from different table. In my case I need either of the two case depending on the scenario. eg:
实际情况
|UserId | First name | Last name | IsRequired | IsDeleted |
1 harry tom true false
1 harry tom false false
3 ram sham true false
场景:
如果有两个记录分别为IsRequired为true和false,那么我需要一个IsRequired为true的情况.
If there are two records on with IsRequired true and false respectively then I need a case with the IsRequired true.
问题:
现在的问题是,我需要过滤掉select语句中的记录.
Now the problem is that I need to filter out the records in the select statement.
所以任何人都知道如何在select语句中做到这一点.
So any one has any idea how to do it in a select statement.
预期病例:
| UserId | First name | Last name | IsRequired | IsDeleted |
1 harry tom true false
2 ram sham true false
推荐答案
更新的解决方案:
SELECT UserId, First name, Last name, IsRequired, IsDeleted FROM table WHERE UserId IN (SELECT UserId FROM users GROUP BY UserId HAVING count(distinct(IsRequired)) > 1) and IsRequired = 'true'
UNION
SELECT UserId, First name, Last name, IsRequired, IsDeleted FROM table WHERE UserId IN (SELECT UserId FROM users GROUP BY UserId HAVING count(IsRequired) = 1)
请参阅 http://sqlfiddle.com/#!3/d178f/1/0
已经复制了完全相同的数据库结构.
Have reproduced the exact same database structure.
这篇关于在重复输入的情况下获取特定的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!