2016.10.23 + 2016.11.02css
MariaDB [tianyuan]> 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 | | Puffball | Diane | hamster | f | 1999-03-30 | NULL | +----------+--------+---------+------+------------+-------+ 4 rows in set (0.00 sec)
MariaDB [tianyuan]> UPDATE pet SET birth = '1994-04-12' WHERE name = 'Buffy';
Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [tianyuan]> 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 | 1994-04-12 | NULL | | Puffball | Diane | hamster | f | 1999-03-30 | NULL | +----------+--------+---------+------+------------+-------+ 4 rows in set (0.00 sec)
編輯文件"1.txt"改正錯誤,而後使用DELETE和LOAD DATA清空並從新裝載表:mysql
MariaDB [tianyuan]> DELETE FROM pet; MariaDB [tianyuan]> LOAD DATA LOCAL INFILE 'pet.txt' INTO TABLE pet;
效果和上面同樣,然而, 若是這樣操作,必須從新輸入Puffball記錄。正則表達式
MariaDB [tianyuan]> select * from pet where name='Buffy';
+-------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+--------+---------+------+------------+-------+ | Buffy | Harold | dog | f | 1994-04-12 | NULL | +-------+--------+---------+------+------------+-------+ 1 row in set (0.00 sec)
MariaDB [tianyuan]> SELECT * FROM pet WHERE birth > '1994-4-12';
+-------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+-------+---------+------+------------+-------+ | yuner | ayuan | person | f | 1997-11-13 | NULL | +-------+-------+---------+------+------------+-------+ 1 row in set (0.00 sec)
MariaDB [tianyuan]> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+-------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+--------+---------+------+------------+-------+ | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------+--------+---------+------+------------+-------+ 1 row in set (0.00 sec)
SELECT * FROM pet WHERE species = 'dog' OR species = 'cat';
+--------+--------+---------+------+------------+-------+ | 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 | +--------+--------+---------+------+------------+-------+ 3 rows in set (0.00 sec)
AND和OR能夠混用,但AND比OR具備更高的優先級。若是你使用兩個操做符,使用圓括號指明如何對條件進行分組是一個好主意。sql
MariaDB [tianyuan]> 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 | +-------+--------+---------+------+------------+-------+ 2 rows in set (0.00 sec)
MariaDB [tianyuan]> SELECT name, birth FROM pet;
+--------+------------+ | name | birth | +--------+------------+ | Fluffy | 1993-02-04 | | Claws | 1994-03-17 | | Buffy | 1989-05-13 | | yuner | 1997-11-13 | +--------+------------+ 4 rows in set (0.00 sec)
MariaDB [tianyuan]> SELECT owner FROM pet;
+--------+ | owner | +--------+ | Harold | | Gwen | | Harold | | ayuan | +--------+ 4 rows in set (0.00 sec)
MariaDB [tianyuan]> SELECT DISTINCT owner FROM pet;
+--------+ | owner | +--------+ | Harold | | Gwen | | ayuan | +--------+ 3 rows in set (0.00 sec)
MariaDB [tianyuan]> SELECT name, species, birth FROM pet -> WHERE species = 'dog' OR species = 'cat';
+--------+---------+------------+ | name | species | birth | +--------+---------+------------+ | Fluffy | cat | 1993-02-04 | | Claws | cat | 1994-03-17 | | Buffy | dog | 1989-05-13 | +--------+---------+------------+ 3 rows in set (0.00 sec)
MariaDB [tianyuan]> SELECT name, birth FROM pet ORDER BY birth;
+--------+------------+ | name | birth | +--------+------------+ | Buffy | 1989-05-13 | | Fluffy | 1993-02-04 | | Claws | 1994-03-17 | | yuner | 1997-11-13 | +--------+------------+ 4 rows in set (0.06 sec)
MariaDB [tianyuan]> SELECT name, birth FROM pet ORDER BY birth DESC;
+--------+------------+ | name | birth | +--------+------------+ | yuner | 1997-11-13 | | Claws | 1994-03-17 | | Fluffy | 1993-02-04 | | Buffy | 1989-05-13 | +--------+------------+ 4 rows in set (0.00 sec)
MariaDB [tianyuan]> select name, species, birth FROM pet -> ORDER BY species, birth DESC;
+--------+---------+------------+ | name | species | birth | +--------+---------+------------+ | Claws | cat | 1994-03-17 | | Fluffy | cat | 1993-02-04 | | Buffy | dog | 1989-05-13 | | yuner | person | 1997-11-13 | +--------+---------+------------+ 4 rows in set (0.06 sec)
MariaDB [tianyuan]> select name, species, birth FROM pet -> ORDER BY species DESC, birth DESC;
+--------+---------+------------+ | name | species | birth | +--------+---------+------------+ | yuner | person | 1997-11-13 | | Buffy | dog | 1989-05-13 | | Claws | cat | 1994-03-17 | | Fluffy | cat | 1993-02-04 | +--------+---------+------------+ 4 rows in set (0.00 sec)
MariaDB [tianyuan]> SELECT name, birth, CURDATE(), -> (YEAR(CURDATE())-YEAR(birth)) -> - (RIGHT(CURDATE(),5)<RIGHT(birth,5)) -> AS age -> FROM pet;
+--------+---------+------------+ | name | species | birth | +--------+---------+------------+ | yuner | person | 1997-11-13 | | Buffy | dog | 1989-05-13 | | Claws | cat | 1994-03-17 | | Fluffy | cat | 1993-02-04 | +--------+---------+------------+ 4 rows in set (0.00 sec)
MariaDB [tianyuan]> select name, birth, death, -> (YEAR(death)-YEAR(birth)) - (Right(death,5)<RIGHT(birth,5)) -> AS age -> FROM pet WHERE death IS NOT NULL ORDER BY age;
+--------+------------+------------+------+ | name | birth | death | age | +--------+------------+------------+------+ | Fluffy | 1993-02-04 | 1995-02-04 | 2 | +--------+------------+------------+------+ 1 row in set (0.03 sec)
MariaDB [tianyuan]> SELECT name, birth FROM pet WHERE MONTH(birth) = 11;
+-------+------------+ | name | birth | +-------+------------+ | yuner | 1997-11-13 | +-------+------------+ 1 row in set (0.00 sec)
MariaDB [tianyuan]> SELECT name, birth FROM pet WHERE MONTH(birth) = (MONTH(birth)+1); Empty set (0.00 sec)
MariaDB [tianyuan]> SELECT name, birth FROM pet WHERE MONTH(birth) = (MONTH(birth)+MONTH(birth)/MONTH(birth)); Empty set (0.07 sec)
MariaDB [tianyuan]> SELECT name, birth FROM pet -> WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));
DATE_ADD( )容許在一個給定的日期上加上時間間隔this
MariaDB [tianyuan]> SELECT name, birth FROM pet -> WHERE MONTH(birth) = MOD(MONTH(CURDATE()), 12) + 1;
獲得的結果是一致的,由於目的同樣。google
+-------+------------+ | name | birth | +-------+------------+ | yuner | 1997-11-13 | +-------+------------+ 1 row in set (0.11 sec)
MariaDB [tianyuan]> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
+----------+-----------+----------+----------+ | 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL | +----------+-----------+----------+----------+ | NULL | NULL | NULL | NULL | +----------+-----------+----------+----------+ 1 row in set (0.00 sec)
MariaDB [tianyuan]> SELECT 1 IS NULL, 1 IS NOT NULL, null IS NULL, null IS NOT NULL;
+-----------+---------------+--------------+------------------+ | 1 IS NULL | 1 IS NOT NULL | null IS NULL | null IS NOT NULL | +-----------+---------------+--------------+------------------+ | 0 | 1 | 1 | 0 | +-----------+---------------+--------------+------------------+ 1 row in set (0.00 sec)
MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE 'y%';
+-------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+-------+---------+------+------------+-------+ | yuner | ayuan | person | f | 1997-11-13 | NULL | +-------+-------+---------+------+------------+-------+ 1 row in set (0.00 sec)
MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE '%r';
+-------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+-------+---------+------+------------+-------+ | yuner | ayuan | person | f | 1997-11-13 | NULL | +-------+-------+---------+------+------------+-------+ 1 row in set (0.00 sec)
MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE '%un%';
+-------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+-------+---------+------+------------+-------+ | yuner | ayuan | person | f | 1997-11-13 | NULL | +-------+-------+---------+------+------------+-------+ 1 row in set (0.00 sec)
MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE '______';(6個下劃線)
+--------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+------------+ | Fluffy | Harold | cat | f | 1993-02-04 | 1995-02-04 | +--------+--------+---------+------+------------+------------+ 1 row in set (0.00 sec)
操做中遇到了問題,暫時跳過,語法較爲簡單,只是須要練習。code
MariaDB [tianyuan]> SELECT * FROM pet WHERE name REGEXP '^.{5}$'; ERROR 1139 (42000): Got error 'this version of PCRE is compiled without UTF support at offset 0' from regexp
MariaDB [tianyuan]> SELECT COUNT(*) FROM pet;
+----------+ | COUNT(*) | +----------+ | 4 | +----------+ 1 row in set (0.00 sec)
MariaDB [tianyuan]> SELECT owner, COUNT(*) FROM pet GROUP BY owner;
+--------+----------+ | owner | COUNT(*) | +--------+----------+ | ayuan | 1 | | Gwen | 1 | | Harold | 2 | +--------+----------+ 3 rows in set (0.00 sec)
MariaDB [tianyuan]> SELECT species, COUNT(*) FROM pet GROUP BY owner;
+---------+----------+ | species | COUNT(*) | +---------+----------+ | person | 1 | | cat | 1 | | cat | 2 | +---------+----------+ 3 rows in set (0.00 sec)
MariaDB [tianyuan]> SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex;
+---------+------+----------+ | species | sex | COUNT(*) | +---------+------+----------+ | cat | f | 1 | | cat | m | 1 | | dog | f | 1 | | person | f | 1 | +---------+------+----------+ 4 rows in set (0.00 sec)
MariaDB [tianyuan]> SELECT species, sex, COUNT(*) FROM pet -> WHERE species = 'dog' OR species = 'cat' -> GROUP BY species, sex;
+---------+------+----------+ | species | sex | COUNT(*) | +---------+------+----------+ | cat | f | 1 | | cat | m | 1 | | dog | f | 1 | +---------+------+----------+ 3 rows in set (0.00 sec)
MariaDB [tianyuan]> SELECT species, sex, COUNT(*) FROM pet -> WHERE sex IS NOT NULL -> GROUP BY species, sex;
+---------+------+----------+ | species | sex | COUNT(*) | +---------+------+----------+ | cat | f | 1 | | cat | m | 1 | | dog | f | 1 | | person | f | 1 | +---------+------+----------+ 4 rows in set (0.00 sec)
MariaDB [tianyuan]> CREATE TABLE event (name VARCHAR(20), date DATE, -> type VARCHAR(15), remark VARCHAR(255));
Query OK, 0 rows affected (0.10 sec)
LOAD DATA LOCAL INFILE 'desktop/event.txt' INTO TABLE event;
MariaDB [tianyuan]> select * from event; +--------+------------+--------+-----------------------------+ | name | date | type | remark | +--------+------------+--------+-----------------------------+ | Fluffy | 1995-05-15 | litter | 4 kittens, 3 female, 1 male | | Buffy | 1993-06-23 | litter | 5 puppies, 2 female, 3 male | | Buffy | 1994-06-19 | litter | 3 puppies, 3 female | | Chirpy | 1999-03-21 | vet | needed beak straightened | | Slim | 1997-08-03 | vet | broken rib | | Bowser | 1991-10-10 | kennel | NULL | +--------+------------+--------+-----------------------------+ 7 rows in set (0.00 sec)
MariaDB [tianyuan]> 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 | -24 | 5 puppies, 2 female, 3 male | | Buffy | -23 | 3 puppies, 3 female | +--------+------+-----------------------------+ 3 rows in set (0.00 sec)