Count Function

COUNT() funktsiyasi belgilangan shartga mos keluvchi satrlar sonini qaytaradi.

SU

SQL COUNT() Funktsiyasi

COUNT() funktsiyasi belgilangan shartga mos keluvchi satrlar sonini qaytaradi.

Sintaksis

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

Misollar

Umumiy Satrlar Sonini Topish Products jadvalidagi jami satrlar sonini topish:

SELECT COUNT(*) AS [Total Rows]
FROM Products;

Demo Ma'lumotlar Bazasi

Quyidagi jadval misol sifatida Products jadvalidan olingan:

ProductIDProductNameSupplierIDCategoryIDUnitPrice
1Chais1110 boxes x 20 bags18
2Chang1124 - 12 oz bottles19
3Aniseed Syrup1212 - 550 ml bottles10
4Chef Anton's Cajun Seasoning2248 - 6 oz jars22
5Chef Anton's Gumbo Mix2236 boxes21.35

Ustunni Ko'rsatish

Asteriks (*) o'rniga ustun nomini ko'rsatishingiz mumkin. Agar ustun nomi ko'rsatilsa, NULL qiymatlar hisobga olinmaydi.

Misol: ProductName ustuni NULL bo'lmagan mahsulotlar sonini topish:

SELECT COUNT(ProductName) AS [Number of Non-NULL Product Names]
FROM Products;

WHERE Klauzasini Qo'shish

Shartlarni belgilash uchun WHERE klauzasini qo'shishingiz mumkin.

Misol: Narxi 20 dan yuqori bo'lgan mahsulotlar sonini topish:

SELECT COUNT(ProductID) AS [Number of Products Over 20]
FROM Products
WHERE Price > 20;

Takrorlarni E'tiborsiz Qoldirish

DISTINCT kalit so'zidan foydalanib, takrorlarni e'tiborsiz qoldirishingiz mumkin. Agar DISTINCT ko'rsatilsa, belgilangan ustundagi bir xil qiymatlarga ega bo'lgan satrlar bir marta hisoblanadi.

Misol: Products jadvalidagi turli narxlar sonini topish:

SELECT COUNT(DISTINCT Price) AS [Number of Different Prices]
FROM Products;

Alias Foydalanish

Hisoblangan ustunga nom berish uchun AS kalit so'zidan foydalaning.

Misol: "Yozuvlar soni" nomini berish:

SELECT COUNT(*) AS [Number of Records]
FROM Products;

COUNT() va GROUP BY bilan Foydalanish

COUNT() funktsiyasini va GROUP BY klauzasini birgalikda ishlatish orqali har bir kategoriya uchun yozuvlar sonini qaytarish mumkin.

Misol: Har bir kategoriya bo'yicha yozuvlar sonini ko'rsatish:

SELECT COUNT(*) AS [Number of Records], CategoryID
FROM Products
GROUP BY CategoryID;

Ushbu sahifada

Xato haqida xabar berish