MySQL/Tutorials: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
imported>Eric Evers
Line 8: Line 8:
===Aggragate Functions===
===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.  
Aggragate functions are functions that combine multiple rows into one output value. Nulls are ignored by aggragate functions unless all the input is NULL in which case the answer is NULL.  


  Table: Product
  Table: Product
Line 18: Line 18:
  |  3 | jam          | 2.75  |
  |  3 | jam          | 2.75  |
  +----+---------------+-------+
  +----+---------------+-------+
====Sum====
====Sum====
  SELECT SUM(Price) as Sum FROM Product;
  SELECT SUM(Price) as Sum FROM Product;
Line 27: Line 28:


====Average====
====Average====
  SELECT AVG(Price) FROM Product;
  SELECT AVG(Price) as "Avg" FROM Product;
  3.00
  +------+
| Avg  |
+------+
| 9.00
+------+
 
====Min====
====Min====
  SELECT MIN(Price) FROM Product;
  SELECT MIN(Price) as "Min" FROM Product;
  2.75
  +------+
| Min  |
+------+
| 2.75
+------+
 
====Max====
====Max====
  SELECT MAX(Price) FROM Product;
  SELECT MAX(Price) as "Max" FROM Product;
  3.25
  +------+
| Max  |
+------+
| 3.25
+------+
 
====Std====
====Std====
  Standard Deviation
  Standard Deviation
   
   
  SELECT STD(Price) FROM Product;
  SELECT STD(Price) AS "Std" FROM Product;
  0.25
  +------+
| Std  |
+------+
| 0.25
+------+


===String functions===
===String functions===

Revision as of 10:04, 10 March 2008

MySQL tutorial

Intall

Create User

Create Database

Queries

Sub Quries

Aggragate Functions

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

Table: Product
+----+---------------+-------+
| Id | Name          | Price |
+----+---------------+-------+
|  1 | bread         | 3.00  |
|  2 | peanut_butter | 3.25  |
|  3 | jam           | 2.75  |
+----+---------------+-------+

Sum

SELECT SUM(Price) as Sum FROM Product;
+------+
| sum  |
+------+
| 9.00 |  
+------+

Average

SELECT AVG(Price) as "Avg" FROM Product;
+------+
| Avg  |
+------+
| 9.00 |  
+------+

Min

SELECT MIN(Price) as "Min" FROM Product;
+------+
| Min  |
+------+
| 2.75 |  
+------+

Max

SELECT MAX(Price) as "Max" FROM Product;
+------+
| Max  |
+------+
| 3.25 |  
+------+

Std

Standard Deviation

SELECT STD(Price) AS "Std" FROM Product;
+------+
| Std  |
+------+
| 0.25 |  
+------+

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