MySQL/Tutorials

From Citizendium
< MySQL
Revision as of 09:42, 10 March 2008 by imported>Eric Evers (→‎Aggragate Functions)
Jump to navigation Jump to search

MySQL tutorial

Intall

Create User

Create Database

Queries

Sub Quries

Aggragate Functions

Aggragate functions are functions that combine multiple rows in to one output value. Nulls are ignored by aggragate functions unless all the input is NULL in which case the answer is NULL.

Sum

SELECT SUM(Price) FROM Products;

Average

SELECT AVG(Price) FROM Products;

Min

SELECT MIN(Price) FROM Products;

Max

SELECT MAX(Price) FROM Products;

STD

SELECT STD(Price) FROM Products;

String functions

Pattern Matching

Using Like
Using RegExp

Regular expressions in SQL

Lets create a simple table.

CREATE TABLE word (name varchar(20));
INSERT into word VALUES ("people"),("places"),("things"),("ppl");
SELECT * FROM word;
+--------+
| name   | 
+--------+
| people |
| places |
| things |
| ppl    |
+--------+

Look for two p's in a row.

SELECT * FROM word WHERE name RegExp "[p]{2}" 
+--------+
| name   | 
+--------+
| ppl    |
+--------+

A "." is any character. A "+" is one or more copies of a character. A "C{n}" looks for n copies of C. Look for two p's but not next to one another.

SELECT * FROM word WHERE name RegExp "p.+p" 
+--------+
| name   | 
+--------+
| people |
+--------+
Exercises

Give a sql regular expression query that will select:

1) only things  
2) only ppl and places
3) only people and places
4) only things and places