看mysql文档笔记

Under Unix, database names are case sensitive (unlike SQL keywords), so you must always refer to your database as menagerie, not as MenagerieMENAGERIE, 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

相关文章
相关标签/搜索