Seperti kita ketahui, untuk menghitung row/baris pada tabel, kita menggunakan fungsi COUNT, yang otomatis menghitung jumlah row yang ada nilainya (row dengan nilai NULL tidak akan dihitung). baris yang dihitung dapat dilakukan dengan 2 cara yaitu: Show
Sedangkan fungsi count dibag menjadi 2 yaitu:
melanjutkan pada artikel sebelumnya, sekarang kita akan menghitung jumlah item yang terjual berdasarkan tahun panjualan, query yang kita gunakan :
yang akan menghasilkan: +----------+----------+----------+----------+ | item_2011| item_2010| jml_2011 | jml_2010 | +----------+----------+----------+----------+ | 8 | 5 | 16124000 | 9955000 | +----------+----------+----------+----------+ selanjutnya jika kita akan mengkelompokkan hasil tersebut berdasarkan field/kolom tertentu misal id_pelanggan dan id_cabang maka kita menggunakan GROUP BY. Contoh: Summary: in this tutorial, you will learn how to use the MySQL 1 function to return the number rows in a table. Introduction to the MySQL INSERT INTO count_demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5);Code language: SQL (Structured Query Language) (sql)1 functionThe 1 function is an aggregate function that returns the number of rows in a table. The 1 function allows you to count all rows or only rows that match a specified condition. The 1 function has three forms: 6, 7 and 8. INSERT INTO count_demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5);Code language: SQL (Structured Query Language) (sql)6 functionThe 6 function returns the number of rows in a result set returned by a 1 statement. The 6 returns the number of rows including duplicate, non-NULL and 3 rows. INSERT INTO count_demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5);Code language: SQL (Structured Query Language) (sql)7The 7 returns the number of rows that do not contain 3 values as the result of the expression. INSERT INTO count_demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5);Code language: SQL (Structured Query Language) (sql)8The 8 returns the number of distinct rows that do not contain 3 values as the result of the expression. The return type of the 1 function is 1. The 1 function returns 0 if there is no matching row found. MySQL INSERT INTO count_demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5);Code language: SQL (Structured Query Language) (sql)1 function illustrationSetting up a sample tableFirst, create a table called 4:
Second, insert some rows into the 4 table:
Third, query data from the 4 table:
MySQL INSERT INTO count_demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5);Code language: SQL (Structured Query Language) (sql)6 exampleThe following statement uses the 6 function to return all rows from the 4 table:
This example uses the 6 function with a 1 clause to specify a condition to count only rows whose value in the column 2 is 2:
MySQL INSERT INTO count_demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5);Code language: SQL (Structured Query Language) (sql)7 exampleIf you specify the 2 column in the 1 function, the 1 function will count only rows with non-NULL values in the 2 column:
Notice that two 3 values are not included in the result. MySQL INSERT INTO count_demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5);Code language: SQL (Structured Query Language) (sql)8 exampleThis example uses 8 to count non-NULL and distinct values in the column 2:
MySQL INSERT INTO count_demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5);Code language: SQL (Structured Query Language) (sql)1 function practical examplesWe’ll use the 3 table from the sample database for the next examples: A) Using MySQL INSERT INTO count_demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5);Code language: SQL (Structured Query Language) (sql)6 function with a SELECT COUNT(val) FROM count_demos;Code language: SQL (Structured Query Language) (sql)5 exampleThe 6 function is often used with a 7 clause to return the number of elements in each group. For example, this statement uses the 1 function with the 5 clause to return the number of products in each product line:
Similarly, this example uses the 6 function to find the number of products supplied by each vendor:
B) Using MySQL INSERT INTO count_demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5);Code language: SQL (Structured Query Language) (sql)6 with a SELECT COUNT(DISTINCT val) FROM count_demos;Code language: SQL (Structured Query Language) (sql)2 clause exampleTo find vendors who supply at least 9 products, you use the 6 function in the 4 clause as shown in the following query:
C) MySQL SELECT COUNT(DISTINCT val) FROM count_demos;Code language: SQL (Structured Query Language) (sql)5 exampleYou can use a control flow expression and functions e.g., 6, 7, and 8 in the 1 function to count rows whose values match a condition. See the following 0 table from the sample database: The following query use 1 with 2 function to find the number of canceled, on hold and disputed orders from the 0 table: 0 The 4 function returns 1 if the order’s status is canceled, on hold or disputed, otherwise, it returns 3. The 6 function only counts 1, not NULL values, therefore, the query returns the number of orders based on the corresponding status. In this tutorial, you have learned various techniques to count the number of rows in a table using the MySQL 6 function. Apa itu Count di mysql?SQL Count() digunakan untuk menampilkan banyaknya data (record) sesuai dengan kriteria tertentu.
Apa itu sum dalam SQL?Fungsi sum() mengembalikan jumlah nilai numerik.
Select distinct untuk apa?Fungsi Distinct mengevaluasi rumus di setiap rekaman tabel dan menghasilkan tabel satu kolom pada dengan nilai duplikat yang dihapus.
Apa itu perintah select?Perintah SELECT adalah perintah yang digunakan untuk mengambil beberapa data yang dibutuhkan. Perintah ini merupakan tipe sintak DML, perintah ini pun digunakan sebagai sebagai perintah dasar untuk memilah informasi dari database.
|