Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

请问这个mysql语句要怎么写?

image

不能使用in 的情况下,要怎么把t1对应t2没有0的数据找出来?
比如这个图 结果是 8、9 H ,因为H没有为0的t2。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

方法1:

SELECT
  `a1`.`*`
FROM
  `test` `a1`
  LEFT JOIN `test` `a2`
    ON `a2`.`t1` = `a1`.`t1`
    AND `a2`.`t2` = 0
WHERE `a2`.`id` IS NULL;

方法2:

SELECT
  *
FROM
  `test`
WHERE `t1` IN
  (SELECT
    `t1`
  FROM
    `test`
  GROUP BY `t1`
  HAVING COUNT(IF(`t2` = 0, 1, NULL)) = 0);

方法3:

SELECT
  `a1`.`*`
FROM
  `test` `a1`
WHERE NOT EXISTS
  (SELECT
    `id`
  FROM
    `test` `a2`
  WHERE `a2`.`t1` = `a1`.`t1`
    AND `a2`.`t2` = 0);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...