shell> mysql -h host -u user -p Enter password: ******** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 25338 to server version: 5.1.2-alpha-standard Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
mysql> SELECT * FROM pet; +----------|--------|---------|------|------------|------------+ | name | owner | species | sex | birth | death | +----------|--------|---------|------|------------|------------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Fang | Benny | dog | m | 1990-08-27 | NULL | | Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 | | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL | | Slim | Benny | snake | m | 1996-04-29 | NULL | | Puffball | Diane | hamster | f | 1999-03-30 | NULL | +----------|--------|---------|------|------------|------------+
查某一行
查询名字叫做 Bowser 的一只宠物的记录:
1 2 3 4 5 6
mysql> SELECT * FROM pet WHERE name = 'Bowser'; +--------|-------|---------|------|------------|------------+ | name | owner | species | sex | birth | death | +--------|-------|---------|------|------------|------------+ | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | +--------|-------|---------|------|------------|------------+
查询哪个动物在 1998 年后出生:
1 2 3 4 5 6 7
mysql> SELECT * FROM pet WHERE birth > '1998-1-1'; +----------|-------|---------|------|------------|-------+ | name | owner | species | sex | birth | death | +----------|-------|---------|------|------------|-------+ | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Puffball | Diane | hamster | f | 1999-03-30 | NULL | +----------|-------|---------|------|------------|-------+
组合条件 AND 和 OR
组合条件,例如,找出雌性的狗:
1 2 3 4 5 6
mysql> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f'; +-------|--------|---------|------|------------|-------+ | name | owner | species | sex | birth | death | +-------|--------|---------|------|------------|-------+ | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------|--------|---------|------|------------|-------+
上面的查询使用AND逻辑操作符,也有一个OR操作符:
1 2 3 4 5 6 7 8
mysql> SELECT * FROM pet WHERE species = 'snake' OR species = 'bird'; +----------|-------|---------|------|------------|-------+ | name | owner | species | sex | birth | death | +----------|-------|---------|------|------------|-------+ | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL | | Slim | Benny | snake | m | 1996-04-29 | NULL | +----------|-------|---------|------|------------|-------+
mysql> SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm') -> OR (species = 'dog' AND sex = 'f'); +-------|--------|---------|------|------------|-------+ | name | owner | species | sex | birth | death | +-------|--------|---------|------|------------|-------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------|--------|---------|------|------------|-------+
mysql> SELECT * FROM pet WHERE name LIKE 'b%'; +--------|--------|---------|------|------------|------------+ | name | owner | species | sex | birth | death | +--------|--------|---------|------|------------|------------+ | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | +--------|--------|---------|------|------------|------------+
要想找出以“fy”结尾的名字:
1 2 3 4 5 6 7
mysql> SELECT * FROM pet WHERE name LIKE '%fy'; +--------|--------|---------|------|------------|-------+ | name | owner | species | sex | birth | death | +--------|--------|---------|------|------------|-------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +--------|--------|---------|------|------------|-------+
要想找出包含“w”的名字:
1 2 3 4 5 6 7 8
mysql> SELECT * FROM pet WHERE name LIKE '%w%'; +----------|-------|---------|------|------------|------------+ | name | owner | species | sex | birth | death | +----------|-------|---------|------|------------|------------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL | +----------|-------|---------|------|------------|------------+
要想找出正好包含5个字符的名字,使用“_”模式字符:
1 2 3 4 5 6 7
mysql> SELECT * FROM pet WHERE name LIKE '_____'; +-------|--------|---------|------|------------|-------+ | name | owner | species | sex | birth | death | +-------|--------|---------|------|------------|-------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------|--------|---------|------|------------|-------+
正则表达式 REGEXP 操作符
由MySQL提供的模式匹配的其它类型是使用扩展正则表达式
[2](#fn1)^。当你对这类模式进行匹配测试时,使用 REGEXP 和 NOT REGEXP
操作符(或RLIKE和NOT RLIKE,它们是同义词)。
为了找出以“b”开头的名字,使用“^”匹配名字的开始:
1 2 3 4 5 6 7
mysql> SELECT * FROM pet WHERE name REGEXP '^b'; +--------|--------|---------|------|------------|------------+ | name | owner | species | sex | birth | death | +--------|--------|---------|------|------------|------------+ | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | +--------|--------|---------|------|------------|------------+
mysql> SELECT * FROM pet WHERE name REGEXP BINARY '^b';
为了找出以“fy”结尾的名字,使用“$”匹配名字的结尾:
1 2 3 4 5 6 7
mysql> SELECT * FROM pet WHERE name REGEXP 'fy$'; +--------|--------|---------|------|------------|-------+ | name | owner | species | sex | birth | death | +--------|--------|---------|------|------------|-------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +--------|--------|---------|------|------------|-------+
为了找出包含一个“w”的名字,使用以下查询:
1 2 3 4 5 6 7 8
mysql> SELECT * FROM pet WHERE name REGEXP 'w'; +----------|-------|---------|------|------------|------------+ | name | owner | species | sex | birth | death | +----------|-------|---------|------|------------|------------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL | +----------|-------|---------|------|------------|------------+
mysql> SELECT * FROM pet WHERE name REGEXP '^.....$'; +-------|--------|---------|------|------------|-------+ | name | owner | species | sex | birth | death | +-------|--------|---------|------|------------|-------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------|--------|---------|------|------------|-------+
你也可以使用“{n}”“重复n次”操作符重写前面的查询:
1 2 3 4 5 6 7
mysql> SELECT * FROM pet WHERE name REGEXP '^.{5}$'; +-------|--------|---------|------|------------|-------+ | name | owner | species | sex | birth | death | +-------|--------|---------|------|------------|-------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------|--------|---------|------|------------|-------+
mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner; +--------|----------+ | owner | COUNT(*) | +--------|----------+ | Benny | 2 | | Diane | 2 | | Gwen | 3 | | Harold | 2 | +--------|----------+
注意: 使用 GROUP BY
对每个owner的所有记录分组,没有它,你会得到错误消息:
1 2 3
mysql> SELECT owner, COUNT(*) FROM pet; ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
下列例子显示出进行动物普查操作的不同方式。
每种动物的数量:
1 2 3 4 5 6 7 8 9 10
mysql> SELECT species, COUNT(*) FROM pet GROUP BY species; +---------|----------+ | species | COUNT(*) | +---------|----------+ | bird | 2 | | cat | 2 | | dog | 3 | | hamster | 1 | | snake | 1 | +---------|----------+
每种性别的动物数量:
1 2 3 4 5 6 7 8
mysql> SELECT sex, COUNT(*) FROM pet GROUP BY sex; +------|----------+ | sex | COUNT(*) | +------|----------+ | NULL | 1 | | f | 4 | | m | 4 | +------|----------+
(在这个输出中,NULL表示“未知性别”。)
按种类和性别组合的动物数量:
1 2 3 4 5 6 7 8 9 10 11 12 13
mysql> SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex; +---------|------|----------+ | species | sex | COUNT(*) | +---------|------|----------+ | bird | NULL | 1 | | bird | f | 1 | | cat | f | 1 | | cat | m | 1 | | dog | f | 1 | | dog | m | 2 | | hamster | f | 1 | | snake | m | 1 | +---------|------|----------+
使用1个以上的表
1 2 3 4 5 6 7 8 9 10 11 12
mysql> SELECT pet.name, -> (YEAR(date)-YEAR(birth)) - (RIGHT(date,5)<RIGHT(birth,5)) AS age, -> remark -> FROM pet, event -> WHERE pet.name = event.name AND event.type = 'litter'; +--------|------|-----------------------------+ | name | age | remark | +--------|------|-----------------------------+ | Fluffy | 2 | 4 kittens, 3 female, 1 male | | Buffy | 4 | 5 puppies, 2 female, 3 male | | Buffy | 5 | 3 puppies, 3 female | +--------|------|-----------------------------+
mysql> SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species -> FROM pet AS p1, pet AS p2 -> WHERE p1.species = p2.species AND p1.sex = 'f' AND p2.sex = 'm'; +--------|------|--------|------|---------+ | name | sex | name | sex | species | +--------|------|--------|------|---------+ | Fluffy | f | Claws | m | cat | | Buffy | f | Fang | m | dog | | Buffy | f | Bowser | m | dog | +--------|------|--------|------|---------+
mysql> SHOW TABLES; +---------------------+ | Tables in menagerie | +---------------------+ | event | | pet | +---------------------+
如果你想要知道一个表的结构,可以使用DESCRIBE命令;它显示表中每个列的信息:
1 2 3 4 5 6 7 8 9 10 11
mysql> DESCRIBE pet; +---------|-------------|------|-----|---------|-------+ | Field | Type | Null | Key | Default | Extra | +---------|-------------|------|-----|---------|-------+ | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | +---------|-------------|------|-----|---------|-------+
补完
使用mysql脚本
1
shell> mysql < batch-file
NULL值
概念上,NULL意味着“没有值”或“未知值”,且它被看作与众不同的值。为了测试NULL,你可以使用IS NULL和IS NOT NULL操作符:
1 2 3 4 5 6
mysql> SELECT 1 IS NULL, 1 IS NOT NULL; +-----------|---------------+ | 1 IS NULL | 1 IS NOT NULL | +-----------|---------------+ | 0 | 1 | +-----------|---------------+
使用用户变量
你可以清空MySQL用户变量以记录结果,不必将它们保存到客户端的临时变量中。
例如,要找出价格最高或最低的物品的,其方法是:
1 2 3 4 5 6 7 8
mysql>SELECT@min_price:=MIN(price),@max_price:=MAX(price) FROM shop; mysql>SELECT*FROM shop WHERE price=@min_price OR price=@max_price; +---------|--------|-------+ | article | dealer | price | +---------|--------|-------+ |0003| D |1.25| |0004| D |19.95| +---------|--------|-------+
根据两个键搜索
可以充分利用使用单关键字的 OR 子句,如同 AND 的处理。
一个比较灵活的例子是寻找两个通过OR组合到一起的关键字:
1 2
SELECT field1_index, field2_index FROM test_table WHERE field1_index ='1'OR field2_index ='1'
该情形是已经优化过的。参见7.2.6节,“索引合并优化”。
还可以使用 UNION 将两个单独的 SELECT 语句的输出合成到一起来更有效地解决该问题。
1 2 3 4 5
SELECT field1_index, field2_index FROM test_table WHERE field1_index ='1' UNION SELECT field1_index, field2_index FROM test_table WHERE field2_index ='1';