Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use COUNT() and DISTINCT together?

Tags:

I would like to count the number of rows from a mysql table and not to include duplicate entries,

Could I use distinct with count()?

like image 449
Adamski Avatar asked Dec 19 '10 16:12

Adamski


People also ask

Can I use distinct with Count in SQL?

The correct syntax for using COUNT(DISTINCT) is: SELECT COUNT(DISTINCT Column1) FROM Table; The distinct count will be based off the column in parenthesis. The result set should only be one row, an integer/number of the column you're counting distinct values of.

Does count Return distinct values?

The COUNT DISTINCT function returns the number of unique values in the column or expression, as the following example shows. SELECT COUNT (DISTINCT item_num) FROM items; If the COUNT DISTINCT function encounters NULL values, it ignores them unless every value in the specified column is NULL.

What effect does distinct have in count *?

SQL COUNT() function with DISTINCT clause eliminates the repetitive appearance of the same data. The DISTINCT can come only once in a given select statement.

How do I count unique rows in SQL?

The COUNT(DISTINCT) function returns the number of rows with unique non-NULL values. Hence, the inclusion of the DISTINCT keyword eliminates duplicate rows from the count. Its syntax is: COUNT(DISTINCT expr,[expr...])

Can I use MySQL count () and distinct together?

Can I use MySQL COUNT () and DISTINCT together? Yes, you can use COUNT () and DISTINCT together to display the count of only distinct rows. To understand the above syntax, let us create a table.

How to use distinct in a SELECT statement?

The DISTINCT can come only once in a given select statement. Syntax : COUNT(DISTINCT expr,[expr...]) Example : To get unique number of rows from the 'orders' table with following conditions - 1. only unique cust_code will be counted, 2. result will appear with the heading "Number of employees", the following SQL statement can be used :

Does count (distinct country) count nulls?

The count (distinct country) query doesn't count null's, but the derived table query does. @a_horse_with_no_name . . .

How to get unique number of rows from the'Orders'table?

To get unique number of rows from the 'orders' table with following conditions -. 1. only unique cust_code will be counted, 2. result will appear with the heading "Number of employees", the following SQL statement can be used : SELECT COUNT ( DISTINCT cust_code ) AS "Number of employees" FROM orders;


2 Answers

Sure.

SELECT COUNT(DISTINCT column) FROM table; 
like image 57
BoltClock Avatar answered Sep 24 '22 01:09

BoltClock


What you need is the following:

SELECT field_type_name, count(*) FROM fields GROUP BY field_type_name; 

This will give you something like this:

image   14 string  75 text     9 
like image 21
Ivan Torres Avatar answered Sep 23 '22 01:09

Ivan Torres