MySQL结果过滤

时间:2013-06-29 13:24:05

标签: mysql

我正在尝试检查我的ID = 4是否在MySQL的结果范围内而不是在最后一行中你可能不明白我要求的内容大声笑,让我现在解释一下:

我有ID = 4硬编码

我有MySQL SELECT语句LIKE:

SELECT * FROM TABLE WHERE GROUP_ID = 10 AND ID = 4 AND (LAST OF THE RECORD FROM THAT GROUP_ID IS NOT (!=) (NOT EQUAL) (IS DIFFERENT) THEN 4

无法解释它更简单

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找这样的查询:

SELECT
  yourtable.GROUP_ID,
  CASE WHEN
    SUM(yourtable.UID=s.max_uid AND yourtable.ID!=4)=1
    AND SUM(yourtable.UID!=s.max_uid AND yourtable.ID!=4)=0
  THEN 'okay' ELSE 'no' END
FROM
  yourtable INNER JOIN
  (SELECT GROUP_ID, MAX(UID) max_uid
   FROM yourtable
   GROUP BY GROUP_ID) s
  ON yourtable.GROUP_ID = s.GROUP_ID
GROUP BY
  yourtable.GROUP_ID

请参阅小提琴here