SQL中MAX 和MIN 的用法

2021-08-15 08:45:33 字數 1042 閱讀 8850

select  max (column_name) / min (column_name)

from table_name

1、查詢teacher表中教師的最大年齡。例項**:

select max (age)  as max_age  from teacher
這樣只能查出來乙個最大值,不能查出教師的id,name等其他資訊。

2、想改進?sql不支援如下的select語句。

select id, name, ***, max (age)

from teacher

因為max()函式屬於聚合函式,處理的是組資料,在本例中,max函式將整個teacher表看成一組,而tname、dname和t***的資料都沒有進行任何分組,因此select語句沒有邏輯意義。

3、下面的**也是無效的。

select id,name, ***,sal ,age

from teacher

where age=max (age)

解決這個問題的方法,就是在where子句中使用子查詢來返回最大值,然後再基於這個返回的最大值,查詢相關資訊。

4、在where子句中使用子查詢返回最大值

查詢teacher表中年紀最大的教師的教工號、姓名、性別等資訊。

例項**:

select id,name, ***, sal, age

from teacher

where age=(select max (age) from teacher)

注意

確定列中的最大值(最小值)時,max( )(min( ))函式忽略null值。但是,如果在該列中,所有行的值都是null,則max( )/min( )函式將返回null值。

mysql中min和max查詢優化

但是往往min 或者max 函式往往會造成全表掃瞄.那麼如何快速查詢出自己想要的資料呢,請看我下面做的測試 首先來看一下表結構 create table biggoods goods id int 10 unsigned not null auto increment,cat id int 10 u...

Linux核心中的Min和Max函式

今天看 時看到乙個有趣的東東,就是linux核心也有min函式,但它的實現很是奇怪,先貼出來 min max macros that also do strict type checking.see the unnecessary pointer comparison.define min x,y ...

C語言 min和max標頭檔案

min和max標頭檔案 雖然說求最大值最小值函式在哪個標頭檔案下並不是非常重要,但是遇到問題的時候我們很快的找到 msdn上說在algorithm下,但是出錯了,其實這兩個函式需要包含兩個標頭檔案和檔案,其他的還有 min和 max需要包含標頭檔案。include iostream include ...