First / Last Letter of a Column

Posted: May 4th, 2009 | Author: Troy | Filed under: MySQL | Tags: | No Comments »

Will return if you have a record beginning with a distinct letter. sort them and count the records.

Useful for alphabetical lists.

For Left (First character):
SELECT DISTINCT LEFT(title. 1) AS letter. COUNT(*) as count FROM table GROUP BY letter ORDER BY count DESC;

For Left (Last character):
SELECT DISTINCT RIGHT(title. 1) AS letter. COUNT(*) as count FROM movies GROUP BY letter ORDER BY count DESC;

(Remember MySQL is optimized for count(*). count(*) actually being faster than count(id))


Finding Duplicates (or records with more than 1 item)

Posted: May 4th, 2009 | Author: Troy | Filed under: MySQL | Tags: | No Comments »

SELECT
DISTINCT id.
COUNT(id)
FROM
my_table
GROUP BY
id
HAVING
COUNT(id) > 1