COUNT() Function SQL

The COUNT function counts the number of rows or records that meet the criteria of the query. For clarity it is wise to use an ALIAS (AS) to designate the result. The following will return a count of all the rows in a table:

SELECT COUNT(*) AS ct_alias FROM table

To find out the number of rows or records in a column that are not null try this:

SELECT COUNT(column) AS ct_alias FROM table

Using DISTINCT in some forms of SQL as the first parameter for COUNT(DISTINCT column) will result in the number of unique cells within a column. There is a way to count rows in mySQL after a mySQL query in PHP:

$sel_items = mysql_query(SELECT * FROM table);
$num_items = mysql_num_rows($sel_items);

When PHP extracts information using a mySQL query the rows are stored in a register which can be accessed one row at a time using mysql_fetch_assoc. This returns an array containing all of the information on one row with the keys being the column names. It is more efficient to use the SQL COUNT function if this is the only bit of information required.

< CONCAT Function SQL | DATEDIFF function SQL >


Send notes in disappearing ink!

Interesting Pages