仅返回符合特定条件的值

发布于 2024-12-21 04:14:38 字数 462 浏览 0 评论 0原文

我有

ID   Rank

11   1  
11   2  
22   1  
22   2  
22   3  
33   1  
33   3  
44   1  
55   1  
55   2  

I Want

11  
44  
55  

即,我只想返回从未处于排名“3”的 ID。

更新 - 除了下面的答案之外,我还想出了另一种更简单的方法来做到这一点。

SELECT ID FROM
(  
  SELECT ID, SUM(CASE WHEN Rank = 3 THEN 1 ELSE 0 END) flag FROM 
  (  
    SELECT ID, Rank FROM T1
  ) a  
  GROUP BY ID    
) b  
WHERE flag = 0;  

I have

ID   Rank

11   1  
11   2  
22   1  
22   2  
22   3  
33   1  
33   3  
44   1  
55   1  
55   2  

I Want

11  
44  
55  

That is, I want to return only the IDs that have never been in a rank of '3'.

UPDATE - I figured another way, rather simpler, to do this in addition to the answer below.

SELECT ID FROM
(  
  SELECT ID, SUM(CASE WHEN Rank = 3 THEN 1 ELSE 0 END) flag FROM 
  (  
    SELECT ID, Rank FROM T1
  ) a  
  GROUP BY ID    
) b  
WHERE flag = 0;  

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

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

发布评论

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

评论(4

日记撕了你也走了 2024-12-28 04:14:38

使用NOT EXISTS

SELECT DISTINCT
    yt1.ID
FROM
    yourTable yt1
WHERE
    NOT EXISTS (
        SELECT *
        FROM yourTable yt2
        WHERE 
            yt1.ID = yt2.ID 
            AND yt2.Rank = 3
    )

或者,使用LEFT JOIN

SELECT DISTINCT
    yt1.ID
FROM
    yourTable yt1
    LEFT JOIN yourTable yt2 ON yt1.ID = yt2.ID AND yt2.Rank = 3
WHERE
    yt2.ID IS NULL

Using NOT EXISTS:

SELECT DISTINCT
    yt1.ID
FROM
    yourTable yt1
WHERE
    NOT EXISTS (
        SELECT *
        FROM yourTable yt2
        WHERE 
            yt1.ID = yt2.ID 
            AND yt2.Rank = 3
    )

Or, using LEFT JOIN:

SELECT DISTINCT
    yt1.ID
FROM
    yourTable yt1
    LEFT JOIN yourTable yt2 ON yt1.ID = yt2.ID AND yt2.Rank = 3
WHERE
    yt2.ID IS NULL
狼亦尘 2024-12-28 04:14:38

我会创建一组唯一的 ID,然后检查这些 ID 的排名是否为 3。那些没有的,将它们放入最终数组中。

$idArray = array();
$noThreeArray = array();

$query = 'SELECT DISTINCT ID FROM yourTable';
while($row = mysql_fetch_array($query)){
   $idArray[] = $row['ID'];
}
foreach($idArray as $id){
   $query = 'SELECT ID FROM yourTable WHERE ID = $id AND Rank = 3';
   if(mysql_num_rows($query) == 0){
      $noThreeArray[] = $row['ID'];
   }
 }

$noThreeArray 现在具有您想要的值。

I would make an array of unique ids, then check those for a rank of three. Those that don't, put them into a final array.

$idArray = array();
$noThreeArray = array();

$query = 'SELECT DISTINCT ID FROM yourTable';
while($row = mysql_fetch_array($query)){
   $idArray[] = $row['ID'];
}
foreach($idArray as $id){
   $query = 'SELECT ID FROM yourTable WHERE ID = $id AND Rank = 3';
   if(mysql_num_rows($query) == 0){
      $noThreeArray[] = $row['ID'];
   }
 }

$noThreeArray now has the values you're after.

随梦而飞# 2024-12-28 04:14:38
SELECT ID FROM
(  
  SELECT ID, SUM(CASE WHEN Rank = 3 THEN 1 ELSE 0 END) flag FROM 
  (  
    SELECT ID, Rank FROM T1
  ) a  
  GROUP BY ID    
) b  
WHERE flag = 0;  
SELECT ID FROM
(  
  SELECT ID, SUM(CASE WHEN Rank = 3 THEN 1 ELSE 0 END) flag FROM 
  (  
    SELECT ID, Rank FROM T1
  ) a  
  GROUP BY ID    
) b  
WHERE flag = 0;  
海拔太高太耀眼 2024-12-28 04:14:38
select ID as IDS from table where rank != 3 group by table.ID;
select ID as IDS from table where rank != 3 group by table.ID;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文