Archive for SQL

Random SQL Results

// November 12th, 2008 // No Comments » // Database, MySQL, PHP, SQL

Last year I needed to display 3 random featured items. The solution was very simple using MySQL with this query:

SELECT * FROM `table` WHERE `column` = 'criteria' ORDER BY RAND() LIMIT 3;

SQL – Counting Occurrances of a String

// October 14th, 2008 // No Comments » // SQL

SELECT DISTINCT
person,
sum(CASE WHEN Field1 = 'String1' THEN 1 ELSE 0 END) AS [Field 1],
sum(CASE WHEN Field2 = 'String2' THEN 1 ELSE 0 END) AS [Field 2],
sum(CASE WHEN Field3 = 'String3' THEN 1 ELSE 0 END) AS [Field 3],
sum(CASE WHEN Field4 = 'String4' THEN 1 ELSE 0 END) AS [Field 4],
sum(CASE WHEN Field5 = 'String5' THEN 1 ELSE 0 END) AS [Field 5]
FROM
tblYourTable
GROUP BY person