我想检查子查询的值如何检查它?

时间:2016-01-15 07:29:43

标签: mysql subquery

这是我在mysql中的更新查询

UPDATE userentries
SET User_Rank = 
(
  SELECT 
  urc.rankofentr as User_Rank
  from UserRankingsys as urc 
  WHERE urc.entryid = userentries.id
  LIMIT 1
) 
where userentries.id = 15

我想检查该部分返回的值

(
  SELECT 
  urc.rankofentr as User_Rank
  from UserRankingsys as urc 
  WHERE urc.entryid = userentries.id
  LIMIT 1
) 

如果此子查询返回大于20而不是我想设置值102

,我该如何做到这一点

1 个答案:

答案 0 :(得分:1)

您可以使用CASE

  SELECT case urc.rankofentr when > 20 then 20 
                             else urc.rankofentr
         end as User_Rank
  from UserRankingsys as urc 
  WHERE urc.entryid = userentries.id
  LIMIT 1

为你sql

UPDATE userentries
SET User_Rank = 
(
 SELECT case urc.rankofentr when > 20 then 20 
                             else urc.rankofentr
         end as User_Rank
  from UserRankingsys as urc 
  WHERE urc.entryid = userentries.id
  LIMIT 1
) 
where userentries.id = 15;