2012-10-18 67 views
1

我正在使用MySQL。選擇最小,最大,平均值,其中

我想選擇最小,最大和平均長度的領域,但我不知道如何。類似這樣的:

SELECT 
    min as min, 
    max as max, 
    avg as avg 
FROM COLUMNS 
WHERE COLUMN_NAME like '%password%' 

任何想法?

+1

你試圖找到_column names_或_column values_的長度統計? –

回答

7
select 
    column_name, 
    min(length(column_name)) as min, 
    max(length(column_name)) as max, 
    avg(length(column_name)) as avg 
from columns 
where column_name like '%password%' 
group by column_name 
+0

這是我想要的東西,但對於返回的每一行。當我做SELECT * FROM COLUMN_NAME LIKE'%password%'時,它返回100條記錄。我如何獲得每個記錄的最小值,最大值和平均值?列COLUMN_NAME包含我數據庫中每列的名稱。 – Alias

+1

@Alias在查詢結尾處添加「GROUP BY COLUMN_NAME」。 –

+0

我改變了答案 –

2
SELECT Min(COLUMN_NAME), 
     Max(COLUMN_NAME), 
     Avg(COLUMN_NAME) 
    FROM TABLENAME 
WHERE COLUMN_NAME LIKE '%password%' 
0
SELECT Min(MENTION_COLUMN_NAME_HERE) AS minimum_value, 
     Max(MENTION_COLUMN_NAME_HERE) AS maximum_value, 
     Avg(MENTION_COLUMN_NAME_HERE) AS average_value 
    FROM COLUMNS 
WHERE COLUMN_NAME LIKE '%passord%' 
相關問題