MySQL/Tutorials: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
imported>Eric Evers
Line 11: Line 11:


Regular expressions in SQL  
Regular expressions in SQL  
--------------------------
Lets create a table.
create table address (name varchar(20));
insert into address values ("people"),("places"),("things"),("ppl");


  table address:
Lets create a simple table.
  CREATE table address (name varchar(20));
INSERT into address VALUES ("people"),("places"),("things"),("ppl");
 
Table address:
  +--------+
  +--------+
  | name  |  
  | name  |  
Line 26: Line 26:
  +--------+
  +--------+


  select * from address where name RegExp "[p]{2}"  
Look for two p's in a row.
 
  SELECT * FROM address WHERE name RegExp "[p]{2}"  
  +--------+
  +--------+
  | name  |  
  | name  |  
Line 33: Line 35:
  +--------+
  +--------+


  select * from address where name RegExp "p.+p"  
A "." is any character.
A "+" is one or more copies of a character.
Look for two p's but not next to one another.
 
  SELECT * FROM address WHERE name RegExp "p.+p"  
  +--------+
  +--------+
  | name  |  
  | name  |  

Revision as of 13:39, 5 March 2008

MySQL tutorial

Intall

Create User

Create Database

Queries

String functions

Pattern Matching

Using Like
Using RegExp

Regular expressions in SQL

Lets create a simple table.

CREATE table address (name varchar(20));
INSERT into address VALUES ("people"),("places"),("things"),("ppl");
Table address:
+--------+
| name   | 
+--------+
| people |
| places |
| things |
| ppl    |
+--------+

Look for two p's in a row.

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

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

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

Give a sql regular expression query that will select:

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