查询指定字段值为最大值和最小值的记录

-- 查询prefix_archives表click字段值最大的记录
-- 备注:若有多条记录的click字段值并列最大,将会把这些记录全部查出
SELECT
  `*`
FROM
  `prefix_archives`
WHERE
    `click` = (
    SELECT
      MAX(`click`)
    FROM
      `prefix_archives`
  );



-- 查询prefix_archives表click字段值最小的记录
-- 备注:若有多条记录的click字段值并列最小,将会把这些记录全部查出
SELECT
  `*`
FROM
  `prefix_archives`
WHERE
    `click` = (
    SELECT
      MIN(`click`)
    FROM
      `prefix_archives`
  );



-- 查询prefix_archives表click字段值最大以及最小的记录
SELECT
  *
FROM
  `prefix_archives`
WHERE
    `click` IN (
                (
                  SELECT
                    MAX(`click`)
                  FROM
                    `prefix_archives`
                ),
                (
                  SELECT
                    MIN(`click`)
                  FROM
                    `prefix_archives`
                )
    );

Copyright © 2024 码农人生. All Rights Reserved