1 Under Unix, database names are case sensitive (unlike SQL keywords), so you must always refer to your database as menagerie, not as Menagerie, MENAGERIE, or some other variant. This is also true for table names. mysql
在unix下,mysql關鍵字是不區分大小寫,但database,table名都是case sensitive。windows不是。但最好the recommended best practice is always to use the same lettercase that was used when the database was created. sql
2 load data windows
mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet -> LINES TERMINATED BY '\r\n'
mysql> INSERT INTO pet -> VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);3 mysql的數據類型
mysql> SELECT * FROM pet WHERE birth >= '1998-1-1';在oracle中要想比較就得如此to_date('1998-1-1','yyyy-MM-dd'),mysql接收的都是字符串,而後再根據比較的field類型進行轉換再比較。
4 表達式 oracle
mysql> SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm') -> OR (species = 'dog' AND sex = 'f');
mysql> SELECT name, birth, CURDATE(), -> TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age -> FROM pet;
To test for NULL, use the IS NULL and IS NOT NULL operators, as shown here: mysql> SELECT 1 IS NULL, 1 IS NOT NULL; +-----------+---------------+ | 1 IS NULL | 1 IS NOT NULL | +-----------+---------------+ | 0 | 1 | +-----------+---------------+
You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL. To demonstrate this for yourself, try the following query: mysql> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL; +----------+-----------+----------+----------+ | 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL | +----------+-----------+----------+----------+ | NULL | NULL | NULL | NULL | +----------+-----------+----------+----------+7 通配符
To find names containing exactly five characters, use five instances of the 「_」 pattern character: 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 | +-------+--------+---------+------+------------+-------+
To find names beginning with 「b」, use 「^」 to match the beginning of the name: 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 | +--------+--------+---------+------+------------+------------+
8 group by this
group by從×中選擇幾個field,而後把這些field distinct放在一塊兒。咱們能夠顯示這些被選擇的值,也能夠對這些被選中的field作一些count,sum等操做。 spa
mysql> 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 | | dog | m | 2 | +---------+------+----------+
SET sql_mode = 'ONLY_FULL_GROUP_BY';
9 join unix
A left join B ,for(A for(B)) code
A right join B , for(B for(A)) 內存
通過上面的比較,可知若是A(記錄1000w條),B(記錄1w條),inner join,選擇A在前呢仍是A在後呢?這個得看機器當時是內存充足型仍是cpu充足型。在都充足的狀況下,我優先選擇用存儲換時間。 ci